1

这是我从活动电子表格中导入一个范围(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 行,这使得它非常慢。

任何帮助将不胜感激。

4

2 回答 2

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;        
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
于 2015-06-17T17:42:40.057 回答
0

如果您想使用对象/关联数组而不是 javascript 数组,请查看ObjService 库。然后你可以做这样的事情:

...
var expense = ObjLib.rangeToObjects(values);

for (var i in expense) {
  if (expense[i].expenseCategory != null) {
    ...
  }
}
于 2013-05-12T01:55:00.793 回答