Google apps script 如何按“排序”;“成本总额”;使用应用程序脚本总计
我正在尝试使用应用程序脚本生成透视表,并按总计中一列的总和进行排序。我总共有三列Google apps script 如何按“排序”;“成本总额”;使用应用程序脚本总计,google-apps-script,google-sheets,pivot-table,google-sheets-api,Google Apps Script,Google Sheets,Pivot Table,Google Sheets Api,我正在尝试使用应用程序脚本生成透视表,并按总计中一列的总和进行排序。我总共有三列 function createCampaignPivotTable() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName(sheetName); var sheetId = sheet.getSheetId(); // The name of the sheet containing the
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
如果使用此配置,我可以按总计对数据透视表进行排序。按我无法按总计中的特定列进行排序
例如,我有
成本总和
,行动总和
,点击总数
。如何使数据透视表按这些列中的一列而不是随机列进行排序?能否提供一份样本表,以便对您的意思有更多的澄清,特别是对于总计部分?对于总计部分,我在第I列有“成本总和”,在第J列有“转换总和”,以及“成本总和”在K列中。如果我使用前面显示的代码,透视表将默认按I列排序。我想要数据透视表按K列排序。嗨!样本表对于测试可能的解决方案非常有用,如果可以,请提供一份。另外,我相信在创建透视表时,您需要在透视表的条件字段中指定所需的列进行排序。更多的是标准本身。让我知道这是否有帮助,或者你是否可以提供样本表:DThanks Mateo。考虑到数据非常敏感,我无法向您提供样本数据。但您可以通过访问图纸的标题。我在这里已经说明了我的问题:当我使用之前提供的代码创建透视表时,我只能对第一列上的透视进行排序,但我想对第K列上的数据进行排序。这是我唯一的要求。而且筛选条件不正确。再次感谢你的帮助。嗨!我请求访问您刚刚共享的资源,您能接受吗?另外,您能否尝试将sourcecolumnsoffset
更改为4,以检查某件事以防万一?非常感谢!