我们有一个数据透视表,将过去3年中每个客户的销售额按年份分组。
我们希望在数据透视表中根据当前年份的销售情况以降序对数据进行排序。
我相信使用PivotGroupSortValueBucket可以实现,但是我不确定如何将这个概念整合到我们已经拥有的东西中。
var pivotTable = Sheets.newPivotTable();
var gridRange = Sheets.newGridRange();
gridRange.sheetId = sourceSheetId;
gridRange.startRowIndex = 0;
gridRange.startColumnIndex = 0;
gridRange.endRowIndex = lastrow;
gridRange.endColumnIndex = 12;
pivotTable.source = gridRange;
var pivotCol0 = Sheets.newPivotGroup()
pivotCol0.sourceColumnOffset = 3;
pivotCol0.showTotals = false;
pivotCol0.repeatHeadings = true
pivotCol0.sortOrder = 'ASCENDING';
var pivotCol1 = Sheets.newPivotGroup()
pivotCol1.sourceColumnOffset = 2;
pivotCol1.showTotals = true;
pivotCol1.repeatHeadings = true
pivotCol1.sortOrder = 'ASCENDING';
var pivotCol2 = Sheets.newPivotGroup()
pivotCol2.sourceColumnOffset = 1;
pivotCol2.showTotals = false;
pivotCol2.repeatHeadings = true
pivotCol2.sortOrder = 'ASCENDING';
var pivotCol3 = Sheets.newPivotGroup()
pivotCol3.sourceColumnOffset = 0;
pivotCol3.showTotals = false;
pivotCol3.repeatHeadings = true
pivotCol3.sortOrder = 'ASCENDING';
var pivotColumns = Sheets.newPivotGroup();
pivotColumns.sourceColumnOffset = 10;
pivotColumns.sortOrder = 'ASCENDING';
pivotColumns.showTotals = true;
pivotTable.columns = pivotColumns;
pivotTable.rows = [pivotCol0,pivotCol1,pivotCol2,pivotCol3]
var pivotValue = Sheets.newPivotValue()
pivotValue.summarizeFunction = 'SUM';
pivotValue.sourceColumnOffset = 9;
pivotTable.values = [pivotValue];
var cellData = Sheets.newCellData();
cellData.pivotTable = pivotTable;
var rows = Sheets.newRowData();
rows.values = cellData;
var start = Sheets.newGridCoordinate();
start.sheetId = targetSheetId;
start.rowIndex = 0;
start.columnIndex = 0;
var updateCellsRequest = Sheets.newUpdateCellsRequest();
updateCellsRequest.rows = rows;
updateCellsRequest.start = start;
updateCellsRequest.fields = 'pivotTable';
// Batch update our spreadsheet
var batchUpdate = Sheets.newBatchUpdateSpreadsheetRequest();
var updateCellsRawRequest = Sheets.newRequest();
updateCellsRawRequest.updateCells = updateCellsRequest;
batchUpdate.requests = [updateCellsRawRequest];
var response = Sheets.Spreadsheets.batchUpdate(batchUpdate, spreadsheetId);