这是我从活动电子表格中导入范围(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])
}
}
}
我的问题:
我知道在这里使用数组是一个坏主意。我想使用类似2D键值对或关联数组(或某些电子表格等效对象)
有人可以指向我可以使用的Google电子表格对象,并说明可以返回2列“费用类别”和SUM(金额)小计的方法。
PS1:我不想使用数据透视表报告,因为我将引入数据透视表不支持的其他基于日期的过滤器。 (见下文) PS2:我目前正在使用电子表格的内置函数“查询”(见下文),但我不喜欢它,因为它不能给我像Pivots那样的总计。 =查询(A2:F;“SELECT SUM(F),D,其中C喜欢'费用'和A&gt; =日期'”&amp; mkdate(I1)&amp;“'和A&lt; = date'”&amp; mkdate (L1)&amp;“'按D顺序分组(F)标签SUM(F)'金额',D'费用类别'”) PS3:我考虑使用像上面这样的Sql查询并在其上运行数据透视表来生成小计,但我觉得它更宽松。此外,我在数据中有大约1000行,这使得它超级慢。
非常感谢任何帮助。
答案 0 :(得分:1)
我构建了一个可能有用的功能。我有一组辅助函数,我在很多电子表格中使用它。但就是这样。
用法:
var DistNTax = SpreadsheetApp
.openById(SheetID)
.getSheetByName('Sheet1')
.getRange('N2:S')
.getValues();
var DistNTax = SumColArray_(DistNTax);
for (var d = 0,LOCNUML=LocNum.length; d < LOCNUML; d++) //Builds 2d Looping-Array to allow choosing of columns at a future point
{
SpreadsheetApp.getActive().getSheetByName('Database').getRange(CurrentEmpty + d,1).setValue(FuncName); //getRange(Row,Col,RowAdd,ColAdd)
SpreadsheetApp.getActive().getSheetByName('Database').getRange(CurrentEmpty + d,2).setValue(TimeFrame); //getRange(Row,Col,RowAdd,ColAdd)
SpreadsheetApp.getActive().getSheetByName('Database').getRange(CurrentEmpty + d,5).setValue(DistNTax[d]); //getRange(Row,Col,RowAdd,ColAdd)
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//Dependent on cleanArray_()
// Array Column Sum Agent
function SumColArray_(sumagent)
{
var newArray = new Array();
for(var i = 0, sL = sumagent.length; i<sL; i++)
{
var totalsum = 0
var CleanForSum = cleanArray_(sumagent[i]);
for(var d = 0, CFSL = CleanForSum.length; d<CFSL; d++)
{
totalsum += CleanForSum[d];
}
newArray.push(Math.round(totalsum));
}
return newArray;
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//Dependent on isEmpty_()
// Blank Array Extractor/Rebuilder
function cleanArray_(actual)
{
var newArray = new Array();
for(var i = 0, aL = actual.length; i<aL; i++)
{
if (isEmpty_(actual[i]) == false)
{
newArray.push(actual[i]);
}
}
return newArray;
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
// Empty String Check
function isEmpty_(string)
{
if(!string) return true;
if(string == '') return true;
if(string === false) return true;
if(string === null) return true;
if(string == undefined) return true;
string = string+' '; // check for a bunch of whitespace
if('' == (string.replace(/^\s\s*/, '').replace(/\s\s*$/, ''))) return true;
return false;
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
答案 1 :(得分:0)
如果您想使用对象/关联数组而不是javascript数组,请查看ObjService Library。然后你可以做这样的事情:
...
var expense = ObjLib.rangeToObjects(values);
for (var i in expense) {
if (expense[i].expenseCategory != null) {
...
}
}