繁体   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