簡體   English   中英

在循環中使用Set-Variable設置子值(例如$ Myvar.item.value)的問題

[英]Issues using Set-Variable to set sub values (ex. $Myvar.item.value) in loop

我正在處理一個腳本,該腳本在幾十個工作表中輸出大約600個變量,並且僅將列標題添加到每個工作表中就需要大約500行代碼。 為了減少大量的整體代碼,我試圖將創建列標題的操作放到一個函數中,但是由於某種原因,set-variable不會設置該值。 當我嘗試在項目中設置子值時,它只會給我一個錯誤。

這將返回以下錯誤

Function PopulateWorkSheet($Worksheet,$Values) {
    ForEach ($Value in $Values) {
        $ColumnCount++
        Set-Variable -Name $($Worksheet).Cells.Items(1,$ColumnCount) -value $Value
    }
    Remove-Variable -Name ColumnCount
}

錯誤

You cannot call a method on a null-valued expression.
At line:4 char:30
+         Set-Variable -Name $($Worksheet).Cells.Items(1,$ColumnCount) -value $Val ...
+                              ~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

但這不會返回錯誤。

Function PopulateWorkSheet($Worksheet,$Values) {
    ForEach ($Value in $Values) {
        $ColumnCount++
        Set-Variable -Name $($Worksheet) -value $Value
    }
    Remove-Variable -Name ColumnCount
}

我的完整代碼

Function PopulateWorkSheet($Worksheet,$Values) {
    ForEach ($Value in $Values) {
        $ColumnCount++
        Set-Variable -Name $($Worksheet).Cells.Items(1,$ColumnCount) -value $Value
    }
    Remove-Variable -Name ColumnCount
}

clear
#Create Excel Com Object
$Excel = New-Object -com Excel.Application

# Make the Excel Application Visible to the end user
$Excel.visible = $True

# Create a WorkBook inside the Excel application
# that we can start manipulating.
$Excel_Workbook = $Excel.Workbooks.Add()

#=======================================================
# Now that we have a workbook we need to create some
# additional worksheets (Tabs) beyond that initial 3
# that are created when the workbook is opened.
#=======================================================
$Temp_MakeWorkSheet = $Excel.Worksheets.Add()

#=======================================================
# Once all the sheets are created each of the worksheets
# need to be assigned to a variable so they can be
# manipulated.
#=======================================================
$Excel_Worksheet_SystemSummary = $Excel.Worksheets.Item(1)
$Excel_Worksheet_SystemSummary.Name = "System Summary"

#=======================================================
# = System Summary = Create Table Headers
#=======================================================

PopulateWorkSheet -Worksheet Excel_Worksheet_SystemSummary -value @("Computer Name","Serial Number","Operating System","Service Pack","OS Architecture","Part of Domain","Domain Role","System Uptime","IP Address","OS HD Smarts Check","# of Mice","# of Keyboards","# of Hard Drives","# of CD/DVD Drives","# of Network Adapters")

為什么您認為根本需要使用Set-Variable Set-Variable用於在您事先不知道名稱或想要執行特殊操作(例如將其設置為只讀)時設置Powershell變量。 在這種情況下,您根本不需要設置Powershell變量,而是要在工作表中設置單元格的值。

所以忘了Set-Variable 還要忘了Remove-Variable :從函數返回仍然會刪除所有局部變量,您不必自己做。

$Worksheet ,如果我正確閱讀了您的代碼,僅是對工作表的引用。 那你為什么寫$($Worksheet) 實際上,這並沒有完成僅編寫$Worksheet本身不會做的任何事情。

現在考慮$Worksheet.Cells.Items(1,$ColumnCount) 前兩個元素不會在循環內更改,因此請將其從循環中提升。 現在,您應該具有以下內容:

Function PopulateWorkSheet($Worksheet,$Values) {
    $cells = $Worksheet.Cells
    $ColumnCount = 0
    ForEach ($Value in $Values) {
        $ColumnCount++
        $cells.Item(1,$ColumnCount) = $Value
    }
}

這也適用:

Function PopulateWorkSheet($Worksheet,$Values) {
    $Worksheet.Range(
        $Worksheet.Cells.Item(1,1),
        $Worksheet.Cells.Item(1,$Values.Count)).value() = $Values
}

並一次性分配完整列表。

代替

Set-Variable -Name $($Worksheet).Cells.Items(1,$ColumnCount) -value $Value

嘗試

$var=Get-Variable -Name $($Worksheet)
$var.Cells.Items(1,$ColumnCount)=$Value

注意...我還沒有嘗試過,但是沒有理由不起作用。

所以我想得太多了。 我不需要嘗試在函數中加載變量,而是將值傳遞給“ Worksheet”變量,因為$ Excel_Worksheet_SystemSummary只是指向單元格內容的鏈接,並且實際上不包含任何實際信息。

Function PopulateWorkSheet($Worksheet,$Values) {
    ForEach ($Value in $Values) {
        $ColumnCount++
        $worksheet.Cells.Item(1,$ColumnCount)=$Value
    }
    Remove-Variable -Name ColumnCount
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM