2014-09-26 62 views
0

我有我需要轉換爲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 

但這並沒有工作。我似乎沒問題,直到我設置了表的第一部分...

+0

定義「不起作用」。請舉一個預期和實際結果的例子。 – 2014-09-26 16:09:17

+0

調用帶有「1」參數的「數據透視表」的異常:「工作表類失敗的數據透視表方法」 在行:1 char:1 + $ Worksheet2.PivotTables(「PivotTable1」)。PivotFields(「State」) +〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 ~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:ComMethodTargetInvocat – 2014-09-26 16:53:10

+0

也不知道如何轉換此代碼在宏... <<選擇結束。 Group Start:= True,End:= True,Periods:= Array(False,False,False,_ False,True,False,True) >> - – 2014-09-26 16:53:57

回答

0

PivotTables("...")PivotTables.Item("...")的簡稱。 PowerShell不允許使用此縮寫,因此您必須在集合上使用Item()方法。更換

$Worksheet2.PivotTables("PivotTable1").PivotFields("...") 

$Worksheet2.PivotTables.Item("PivotTable1").PivotFields.Item("...") 

至於翻譯方法調用帶有命名參數,您需要提供正確的順序所有參數。使用[Type]::Missing作爲應保留其默認值的參數。儘管如此,後面的默認值列表可以省略。在你的情況下,線

Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _ 
    False, True, False, True) 

或許應該成爲這樣的事:很多發展和安斯加爾提供的答案後

$periods = @($false, $false, $false, $false, $true, $false, $true) 
$selection.Group($true, $true, [Type]::Missing, $periods) 
+0

當我添加「.Item」我得到這個錯誤,我重新加載並重試>>方法調用失敗,因爲[System.Management.Automation.PSMethod]不包含名爲'Item'的方法。 在線:3 char:1 + $ PivotFields = $ Worksheet2.PivotTables.Item(「PivotTable1」)。PivotFields.Item(「Sta ... + ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:MethodNotFound << – 2014-09-26 17:18:38

0

確定。我有完整的代碼。我已將信息更改爲「通用化」代碼....謝謝大家的幫助。此代碼有效並且完整。

# Select the cells. 
$Worksheet2.Cells.Item(1,1).Select() 
$WorkBook.ShowPivotTableFieldList = $true 

#$PivotFields = $Sheet1.PivotTables("PivotTable1").PivotFields($columnFields) 
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("Field1") # ColumnLables  = "State" 
$PivotFields.Orientation = $xlColumnField 
$PivotFields.Position = 1 
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("Field2") # ColumnLables = "Submit Date" 
$PivotFields.Orientation = $xlRowField 
$PivotFields.Position = 1 
$PivotFields = $Worksheet2.PivotTables("PivotTable1").PivotFields("Field3") # ColumnLables = Actual Completion Date" 
$PivotFields.Orientation = $xlDataField 
$PivotFields.Position = 1 
# Set the range of the group. 
$range22 = $excel.Range("a7") 
# $range22.Group() # To test the basic group. 
# Here is where the grouping is done. 
$periods = @($false, $false, $false, $false, $true, $false, $true) 
$range22.Group($true, $true, [Type]::Missing, $periods)