我有一个创建数据透视表的脚本,但我不想显示小计,只显示最终总计。
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getActiveWorksheet(); // Downloading the active sheet
// Finding the range with the data (I assume it starts with A1)
let dataRange = sheet.getRange("A1").getSurroundingRegion();
// Checking that the scope is correct
if (!dataRange) {
console.log("No data found.");
return;
}
// Creating a pivot table on the same sheet
let pivotTable = sheet.addPivotTable("February", dataRange, sheet.getRange("H1"));
let rowHierarchy = pivotTable.addRowHierarchy(pivotTable.getHierarchy("Posting date"));
// Pivot table configuration
pivotTable.addColumnHierarchy(pivotTable.getHierarchy("Project no."));
pivotTable.addColumnHierarchy(pivotTable.getHierarchy("Name"));
pivotTable.addColumnHierarchy(pivotTable.getHierarchy("Task No."));
// Dodanie wartości (Ilość)
let qtyHierarchy = pivotTable.getHierarchy("Time");
let dataField = pivotTable.addDataHierarchy(qtyHierarchy);
dataField.setSummarizeBy(ExcelScript.AggregationFunction.sum);
// Hiding subtotals for "Project No." and "Name"
// Alternate formatting (lines)
let pivotRange = pivotTable.getLayout().getRange();
let rowCount = pivotRange.getRowCount();
for (let i = 1; i < rowCount; i += 2) { // Every second line
let row = pivotRange.getCell(i, 0).getEntireRow();
row.getFormat().getFill();
}
console.log("The pivot table has been created.");
}
我希望不显示总计,但希望显示列线和行线。
设置和隐藏总计的方法通过 PivotLayout 接口公开。
PivotTable 接口,getLayout() 方法
PivotLayout 接口,setShowRowGrandTotals 方法