【发布时间】:2019-01-03 03:25:00
【问题描述】:
所以我已经有了在数据透视表中表达电子表格数据的代码,但我需要将这些值显示为列总数的百分比。似乎有一个属性可以实现该结果,但我不知道如何将其集成到脚本中。
https://developers.google.com/apps-script/reference/spreadsheet/pivot-value-display-type
function addPivotTable3(spreadsheetId3, pivotSourceDataSheetId3, destinationSheetId3)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = "Sheet3";
var pivotTableParams = {};
// The source indicates the range of data you want to put in the table.
// optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex
pivotTableParams.source = {
sheetId: ss.getSheetByName(sheetName).getSheetId()
};
// Group rows, the 'sourceColumnOffset' corresponds to the column number in the source range
// eg: 0 to group by the first column
pivotTableParams.rows = [{
sourceColumnOffset: 2,
sortOrder: "ASCENDING"
}];
// Defines how a value in a pivot table should be calculated.
pivotTableParams.values = [{
summarizeFunction: "SUM",
displayType: "PERCENT_OF_COLUMN_TOTAL",
sourceColumnOffset: 3
}];
var requests = [{
'updateCells': {
'rows': {
'values': [
{
'pivotTable': {
'source': {
'sheetId': pivotSourceDataSheetId3,
'startRowIndex': 0,
'startColumnIndex': 0,
'endRowIndex': 94,
'endColumnIndex': 4,
},
'rows': [
{
'sourceColumnOffset': 2,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {
'buckets': [
{
'stringValue': 'BAE Stages',
},
],
},
},
{
'sourceColumnOffset': 94,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {},
},
],
'columns': [
{
'sourceColumnOffset': 0,
'sortOrder': 'ASCENDING',
'showTotals': true,
'valueBucket': {},
},
],
'values': [
{
'summarizeFunction': "SUM",
'sourceColumnOffset': 3,
//'displayType': "PERCENT_OF_COLUMN_TOTAL",
//This line triggers a JSON erorr
},
],
'valueLayout': 'HORIZONTAL',
},
},
],
},
'start': {
'sheetId': destinationSheetId3,
'rowIndex': 0,
'columnIndex': 0,
},
'fields': 'pivotTable',
},
}];
var response =
Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId3);
}
【问题讨论】:
-
使用新的 API 端点时,使用 Google APIs Explorer 以交互方式构建有效的请求正文。它包括悬停工具提示和“字段”生成器
标签: google-apps-script google-sheets