google-apps-script - 如何使用应用程序脚本按总计中的“成本总和”排序
问题描述
我正在尝试使用应用程序脚本生成数据透视表并按总计中一列的总和进行排序。我总共有三列。
function createCampaignPivotTable() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// The name of the sheet containing the data you want to put in a table.
var pivotTableParams = {};
// var customerId = getSourceTable();
// The source indicates the range of data you want to put in the table.
// optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex
pivotTableParams.source = {
sheetId: sheetId
};
// Group rows, the 'sourceColumnOffset' corresponds to the column number in the source range
// eg: 0 to group by the first column
pivotTableParams.columns = [
{
sourceColumnOffset: 4,
sortOrder: "ASCENDING",
"showTotals": true,
"valueBucket": {
"buckets": [
{
"stringValue": "Ad Destination"
}
]
}
}
];
pivotTableParams.rows = [
{
sourceColumnOffset: 0,
sortOrder: "ASCENDING",
"showTotals": true,
"valueBucket": {
"buckets": [
{
"stringValue": "Customer ID"
}
]
}
},
{
sourceColumnOffset: 2,
sortOrder: "DESCENDING",
"showTotals": true,
"valueBucket": {
"buckets": []
}
}
];
// Defines how a value in a pivot table should be calculated.
pivotTableParams.values = [
{
summarizeFunction: "SUM",
sourceColumnOffset: 5
},
{
summarizeFunction: "SUM",
sourceColumnOffset: 6
},
{
summarizeFunction: "SUM",
sourceColumnOffset: 7
},
];
// Create a new sheet which will contain our Pivot Table
var pivotsheet = ss.getSheetByName('Pivot Table - Campaigns');
if (pivotsheet){
ss.deleteSheet(pivotsheet);}
var pivotTableSheet = ss.insertSheet('Pivot Table - Campaigns');
var pivotTableSheetId = pivotTableSheet.getSheetId();
// Add Pivot Table to new sheet
// Meaning we send an 'updateCells' request to the Sheets API
// Specifying via 'start' the sheet where we want to place our Pivot Table
// And in 'rows' the parameters of our Pivot Table
var request = {
"updateCells": {
"rows": {
"values": [{
"pivotTable": pivotTableParams
}]
},
"start": {
"sheetId": pivotTableSheetId
},
"fields": "pivotTable"
}
};
Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
return pivotTableSheetId
如果我使用此配置,我可以按总计对我的数据透视表进行排序。我无法按总计中的特定列排序。
例如,我有sum of cost
, sum of action
,sum of clicks
在Grand Total
. 如何使数据透视表按这些列之一而不是随机列排序?
解决方案
推荐阅读
- ios - 如何确定从用户位置 (CLLocationCoordinate2D) 到 CGPath 的距离?
- python - 我怎样才能创建这样的功能?
- eslint - 故事书 mdx 的 eslint
- sql - 按日期获取前 3 条记录 SQL Server 2012
- r - 闪亮的网站图标使用 URL 但不是本地文件路径
- python - Python - 为我的字典列表写入 txt 文件
- html - 如何换行或拆分已经在 CSS 中包含多个连字符的单词?
- python - 如何有效地合并二维上的两个 3d 数组?
- sed - 如何批量更新所有符号链接指针以用空字符串替换“_F-ss”?
- php - 使用 Curl 登录 Codeigniter 不起作用?