這裏是我的函數從活動電子表格中導入一個範圍(6列;大量的行),並返回第5列分組的小計。Google電子表格腳本返回小計
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這樣的子總計的總計。 C像'費用'和A> =日期'「(F)'SUM'(F)'金額',D'費用類別') PS3 :我考慮使用類似上面的Sql查詢並在其上運行數據透視表來生成小計,但我覺得自己很鬆散。除此之外,我在數據中有大約1000行,這使得它非常慢。
任何幫助將不勝感激。