powershell Call method with optional arguments

后端 未结 2 1149
再見小時候
再見小時候 2020-12-06 19:59

I have an excel vba code that finds a particular cell in a sheet. It uses the Find method form the excel libraries. Here is the code

objRange.Fi         


        
2条回答
  •  误落风尘
    2020-12-06 20:41

    I addressed this by creating overload methods that calls the core method with the value I want. e.g. I want the user to be able to specify the number of processed records or call the "Increment" method and then just call "WriteProgress" with no argument:

    class UserFeedback {
    [string]$Name
    [int]$ThingCount
    [datetime]$ProcessStartDateTime
    [int]$ProcessedCount
    
    UserFeedback ([string] $Name,[int]$ThingCount){
        $this.ProcessStartDateTime = Get-Date
        $this.Name = $Name
        $this.ThingCount = $ThingCount
        $this.ProcessedCount = 0
    }
    
    WriteProgress([int] $intProcessed){
        $this.ProcessStartDateTime        
        $SecondsElapsed = ((Get-Date) - $this.ProcessStartDateTime).TotalSeconds
        $SecondsRemaining = ($SecondsElapsed / ($intProcessed / $this.ThingCount)) - $SecondsElapsed
        Write-Progress -Activity $this.Name -PercentComplete (($intProcessed/$($this.ThingCount)) * 100) -CurrentOperation "$("{0:N2}" -f ((($intProcessed/$($this.ThingCount)) * 100),2))% Complete" -SecondsRemaining $SecondsRemaining
    }
    
    WriteProgress(){
        $this.WriteProgress($this.ProcessedCount)
    }
    
    Increment(){
        $this.ProcessedCount ++    
    }
    

    }

提交回复
热议问题