edi*_*dna 1 vba pivot-table office365 office-scripts microsoft365
我正在尝试在 Office 脚本中重建一些 VBA 代码,因为我想通过 Microsoft Power Automate(以前的 Flow)自动执行。现在,我的处理数据透视表的代码部分遇到了问题。在 VBA 代码中,我使用:
Dim oWSHilfsPivot As Worksheet
Set oWSHilfsPivot = ActiveWorkbook.Sheets("Hilfs_Pivots")
With oWSHilfsPivot.PivotTables("PivotTable1").PivotFields("Projekt")
.PivotItems("(blank)").Visible = True
End With
Run Code Online (Sandbox Code Playgroud)
如何在 Office 脚本中重建过滤?当我尝试使用集成的 Office 脚本记录器记录它时,我得到:
function main(workbook: ExcelScript.Workbook) {
// Unknown event received with eventId:153
}
Run Code Online (Sandbox Code Playgroud)
因此,默认情况下,Office 脚本似乎还不支持此功能。当然,变量的定义有效,过滤是问题所在。;)
我很感激任何帮助。
在数据透视表中,您需要: .getRowHierarchy 和 .getPivotItem,然后将可见性设置为 false(如下所示)。
下面的代码示例
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("DIM_INSP").setVisible(false);
Run Code Online (Sandbox Code Playgroud)
function main(workbook: ExcelScript.Workbook) {
// Add a new worksheet
let sheet1 = workbook.addWorksheet();
let selectedSheet = workbook.getWorksheet("New Sheet");
// Add a new pivot table
let newPivotTable = workbook.addPivotTable("PivotTable1", selectedSheet.getTable("MSRLoop"), sheet1.getRange("A3:C20"));
// Add pivot field to a hierarchy
newPivotTable.addRowHierarchy(newPivotTable.getHierarchy("Work Center"));
// Change pivot position in a hierarchy
newPivotTable.getRowHierarchy("Work Center")
.setPosition(0);
// Add pivot field to a hierarchy
newPivotTable.addRowHierarchy(newPivotTable.getHierarchy("Order Type"));
// Change pivot position in a hierarchy
newPivotTable.getRowHierarchy("Order Type")
.setPosition(1);
// Add pivot field to a hierarchy
newPivotTable.addDataHierarchy(newPivotTable.getHierarchy("Required Capacity"));
// Change pivot position in a hierarchy
newPivotTable.getDataHierarchy("Sum of Required Capacity")
.setPosition(0);
// Add pivot field to a hierarchy
newPivotTable.addColumnHierarchy(newPivotTable.getHierarchy("Prdn Week"));
// Change pivot position in a hierarchy
newPivotTable.getColumnHierarchy("Prdn Week")
.setPosition(0);
//HERE IS HOW YOU FILTER IN A PIVOT TABLE
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("DIM_INSP").setVisible(false);
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("FIN_INSP").setVisible(false);
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("FLAT_INSP").setVisible(false);
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("WELD_INSP").setVisible(false);
newPivotTable.getRowHierarchy("Work Center")
.getPivotField("Work Center").getPivotItem("(blank)").setVisible(false);
}
Run Code Online (Sandbox Code Playgroud)