我有我需要轉換爲PowerShell的數據透視代碼,但我只是沒有弄清楚。任何人都可以幫助將此轉換編碼到PowerShell中嗎?將數據透視表代碼從VBA轉換到PowerShell
這是Excel的VB宏代碼:
Sub XX()
'
' XX Macro
Sheets("PivotSheet").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R2C2:R1680C10", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="PivotSheet!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion14
Sheets("PivotSheet").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("State")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Submit Date")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Actual Completion Date")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("State")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_
"PivotTable1").PivotFields("Actual Completion Date"), _
"Count of Actual Completion Date", xlCount
Range("A8").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, True)
End Sub
以下是我對PowerShell的創建:
[void]PivotSheet.Select()
$PivotRange = "Data!R2C2:J$lastrow"+'C10'
$PivotTable = $WorkBook.PivotCaches().Create($xlDatabase,$PivotRange,$xlPivotTableVersion14)
$PivotTable.CreatePivotTable("PivotSheet!R1C1","PivotTable1") | Out-Null
Start-Sleep -Milliseconds 500
[void]PivotSheet.Select()
$WorkSheet2.activate() | Out-Null
$Worksheet2.Cells.Item(1,1).Select()
$WorkBook.ShowPivotTableFieldList = $true
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("State")
$PivotFields.Orientation = $xlRowField
$PivotFields.Position = 1
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("Submit Date")
$PivotFields.Orientation = $xlRowField
$PivotFields.Position = 2
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("Actual Completion Date")
$PivotFields.Orientation = $xlRowField
$PivotFields.Position = 3
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("State")
$PivotFields.Orientation = $xlColumnField
$PivotFields.Position = 1
$PivotFields = $Worksheet2.PivotTables("PivotTable1").AddDataField($Worksheet2.PivotTables,
("PivotTable1").PivotFields("Actual Completion Date"), "Count of Actual Completion Date", $xlCount)
$PivotFields.Range("A8").Select
但這並沒有工作。我似乎沒問題,直到我設置了表的第一部分...
定義「不起作用」。請舉一個預期和實際結果的例子。 – 2014-09-26 16:09:17
調用帶有「1」參數的「數據透視表」的異常:「工作表類失敗的數據透視表方法」 在行:1 char:1 + $ Worksheet2.PivotTables(「PivotTable1」)。PivotFields(「State」) +〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 ~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:ComMethodTargetInvocat – 2014-09-26 16:53:10
也不知道如何轉換此代碼在宏... <<選擇結束。 Group Start:= True,End:= True,Periods:= Array(False,False,False,_ False,True,False,True) >> - – 2014-09-26 16:53:57