应该如何编辑数据透视表代码,以便在下面获得所需的输出?
Desired output 2 - multi-column
的示例我在代码的后半部分遇到问题-
//定义要在数据透视表中的列引用的数据源列
在代码中,我正在定义列0,即年份 我需要将其细分为年份(来源第0列)和年份(来源第1列)。
//定义如何在数据透视表以及应该从中获取值的数据源列中表达值
在下面的代码中,我仅选择了第3列 我需要第3列和第4列的值之和,分别表示。
function addPivotTable1(spreadsheetId1, pivotSourceDataSheetId1, destinationSheetId1)
//workbook id, data source sheet id, destination sheet id
{
var requests = [{
'updateCells': {
'rows': {
'values': [
{
'pivotTable': {
'source': {
//define cells in data source sheet, starting with 0,0 for cell A1
'sheetId': pivotSourceDataSheetId1,
'startRowIndex': 0,
'startColumnIndex': 0,
'endRowIndex': 463,
'endColumnIndex': 4,
},
'rows': [
//define column in source sheet that will be in pivot table row
{
'sourceColumnOffset': 2,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {
'buckets': [
{
'stringValue': 'Number of Accounts',
},
],
},
},
//define last row in the source column for reference
{
'sourceColumnOffset': 463,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {},
},
],
//define data source column to be referenced for columns in pivot table
'columns': [
{
'sourceColumnOffset': 0,
'sortOrder': 'ASCENDING',
'showTotals': true,
'valueBucket': {},
},
],
//define how values should be expressed in pivot table, as well as data source column where values should be taken from
'values': [
{
'summarizeFunction': 'SUM',
'sourceColumnOffset': 3,
},
],
'valueLayout': 'HORIZONTAL',
},
},
],
},
//define cell in destination sheet where pivot table should be placed, 0,0 for top of sheet
'start': {
'sheetId': destinationSheetId1,
'rowIndex': 0,
'columnIndex': 0,
},
'fields': 'pivotTable',
},
}];
var response =
Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId1);
}