这是我从活动电子表格中导入一个范围(6 列;和很多行)并返回按第 5 列分组的小计的函数。
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getRange("A3:F");
var values = rows.getValues();
var expense = new Array();
var expensehead = new Array();
for (var i = 0, e = 0; i <= rows.getNumRows()-1; i++) {
if (values[i][0] instanceof Date & values[i][2] == "Expense") { // As long as col0 is a date and col2 == "Expense"
if (expense.hasOwnProperty(values[i][4])) {
// if the index "Expense Category" (col4) already exists in the array, add the amount (col 5) to the existing value
// Add amount (col 5) array expense where index is "Expense Category" (col4)
// For example Expense['Food'] = Expense['Food'] + 100; Emulating it like an associative array here
expense[values[i][4]]= expense[values[i][4]] + values[i][5];
}
else {
// The index "Expense Category" (col4) does already exists in the array, assign the amount (col 5) to the new index
// Add amount (col 5) array expense where index is "Expense Category" (col4)
// For example Expense['Food'] = 100; I have spet on food for the first time, hence it does not exist in the array already. Emulating it like an associative array here
expense[values[i][4]]= values[i][5];
//Since Javascript or Google script does not support iteration in an associative array,
//I am adding all indexes to another array expensehead so i will be able to pull out the info later.
expensehead.push(values[i][4])
}
}
}
我的问题:
我知道在这里使用数组是个坏主意。我想使用二维键值对或关联数组(或某些电子表格等效对象)之类的东西
有人可以指向我可以使用的 Google 电子表格对象,还可以说明可以返回 2 列“费用类别”和 SUM(金额)小计的方法。
PS1:我不想使用数据透视表报告,因为我将引入数据透视表不支持的其他基于日期的过滤器。(见下文) PS2:我目前正在使用电子表格的内置功能“查询”(见下文),但我不喜欢它,因为它不能像 Pivots 那样给我小计的总计。=query(A2:F;"SELECT SUM(F), D where C like 'Expense' AND A >= date '"& mkdate(I1) &"' AND A <= date '"& mkdate(L1) &" ' group by D Order by SUM(F) Label SUM(F) 'Amount', D 'Expense Category' ") PS3:我考虑过使用上面的 Sql 查询并在其上运行数据透视表来生成小计,但我觉得像一个宽松的。此外,我的数据中有大约 1000 行,这使得它非常慢。
任何帮助将不胜感激。