如何以编程方式将求和列从Rows移动到数据透视表中的列[Powershell]

时间:2017-07-05 15:48:59

标签: excel powershell pivot-table

我一直在使用powershell根据磁盘空间报告生成数据透视表。下面是我用来生成数据透视表的脚本的一部分

# Get sheets
$ws3 = $workbook.worksheets | where {$_.name -eq "DiskInformation"} #<------- Selects sheet 3


$xlPivotTableVersion12     = 3
$xlPivotTableVersion10     = 1
$xlCount                 = -4112
$xlDescending             = 2
$xlDatabase                = 1
$xlHidden                  = 0
$xlRowField                = 1
$xlColumnField             = 2
$xlPageField               = 3
$xlDataField               = 4    
$xlDirection        = [Microsoft.Office.Interop.Excel.XLDirection]
# R1C1 means Row 1 Column 1 or "A1"
# R65536C5 means Row 65536 Column E or "E65536"

$range1=$ws3.range("A1")

$range1=$ws3.Range($range1,$range1.End($xlDirection::xlDown))
$range2=$ws3.range("H1")
$range2=$ws3.Range($range2,$range2.End($xlDirection::xlDown))
$selection = $ws3.Range($range1, $range2)
#Write-Host "R2:"$range2
$PivotTable = $workbook.PivotCaches().Create($xlDatabase,$selection,$xlPivotTableVersion10)
$PivotTable.CreatePivotTable("R6C1","Tables1") | Out-Null 
[void]$ws3.Select()
#$ws3.Cells.Item(3,1).Select()
$workbook.ShowPivotTableFieldList = $true 




$PivotFields = $ws3.PivotTables("Tables1").PivotFields("Environment")
$PivotFields.Orientation = $xlColumnField

$PivotFields = $ws3.PivotTables("Tables1").PivotFields("Used Capacity(GB)")
$PivotFields.Orientation = $xlDataField


$PivotFields = $ws3.PivotTables("Tables1").PivotFields("Free Space(GB)")
$PivotFields.Orientation = $xlDataField


$PivotFields = $ws3.PivotTables("Tables1").PivotFields("Farms/Classification")
$PivotFields.Orientation = $xlRowField

以上脚本取自enter link description here

上面的代码工作得非常好,它会生成报告,但它会生成一个求和行“值”,我需要它在列部分。请在下面找到截图,了解它当前的样子

StackOverFlow Site

您能告诉我如何将自动生成的字段“值”从行移动到列部分吗?

1 个答案:

答案 0 :(得分:1)

对于以后的参考,以下代码对我有用

$PivotFields = $PivotTable.PivotTables("Tables1").DataPivotField
$PivotFields.Orientation = $xlColumnField
$PivotFields.Position = 1