2014-01-26 22 views
0

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

這將返回以下錯誤

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") 

回答

0

而不是

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

嘗試

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

注...我還沒有試過,但沒有理由它不會工作。

+0

不幸的是,使用這種方法使得它認爲變量爲空:/「你不能在空值表達式上調用方法。」 – user1451070

+0

我的壞...我沒有閱讀整個列表。不幸的是這些解決方案的 –

1

爲什麼你認爲你需要使用Set-VariableSet-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 
} 

,並一氣呵成分配的完整列表。

+0

工作:/ – user1451070

+0

@ user1451070他們應該現在的工作(我沒有說我沒有Excel的手):你寫的'Items'(我複製)的方法實際上只是'Item'和範圍分配的作品,但只有當你明確地說你正在分配給'value()'屬性。 – Duncan

0

所以我是這麼想的。我不需要嘗試在函數中加載變量,而是將值傳遞給「Worksheet」變量,因爲$ Excel_Worksheet_SystemSummary只是單元格內容的鏈接,並且它實際上並不包含任何真實信息。

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