Excel:从脚本创建枢轴,允许'值过滤'

时间:2018-05-28 07:19:05

标签: excel powershell pivot

我使用Powershell脚本将数据透视表添加到现有的Excel工作簿中。

但是,我无法添加值过滤器(我使用计数功能) - 至少使用Excel用户界面是不可能的。

如果我使用Excel的UI创建数据透视表,一切都可以正常工作。 那么,程序化创建的数据透视与创建的数据库之间的区别是什么?

程序化支点 缺少值过滤器选项:Programmatic pivot: the _missing_ value filter option

UI已创建数据透视值过滤器可用:UI created pivot: value filter is available

我的代码:

$xlRowField = 1
$xlColumnField = 2
$xlDataField = 4
$xlCount = -4112

$Excel.Visible = $True
$outputWorkBook = $Excel.Workbooks.Open("FILENAME.xlsx", $True, $True)
pivotTable = $outputWorkBook.ActiveSheet.PivotTableWizard()

$pivotTable.PivotFields("id").Orientation = [int]$xlRowField
$pivotTable.PivotFields("filename").Orientation = [int]$xlColumnField

$pivotDataCount = $pivotTable.PivotFields("id")
$pivotDataCount.Orientation = [int]$xlDataField
$pivotDataCount.Function = [int]$xlCount

生成的代码为VBA宏,有效:

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets.Add
    ActiveWorkbook.Worksheets("Sheet2").PivotTables("PivotTable1").PivotCache. _
        CreatePivotTable TableDestination:="Sheet4!R3C1", TableName:="PivotTable3" _
        , DefaultVersion:=6
    Sheets("Sheet4").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable3")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable3").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("id")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("filename")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
        "PivotTable3").PivotFields("id"), "Sum of id", xlSum
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Sum of id")
        .Caption = "Count of id"
        .Function = xlCount
    End With
    Range("A5").Select
    ActiveSheet.PivotTables("PivotTable3").PivotFields("id").PivotFilters.Add2 _
        Type:=xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables( _
        "PivotTable3").PivotFields("Count of id"), Value1:=1
End Sub

1 个答案:

答案 0 :(得分:0)

我采用了(临时)解决方法。 我只是暂停脚本并要求用户手动创建数据透视表。 然后,脚本配置透视表incl。价值过滤器。