2013-05-05 19 views
1

這裏是我的函數從活動電子表格中導入一個範圍(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行,這使得它非常慢。

任何幫助將不勝感激。

回答

0

如果您想使用對象/關聯數組而不是javascript數組,請查看ObjService Library。然後你可以這樣做:

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

for (var i in expense) { 
    if (expense[i].expenseCategory != null) { 
    ... 
    } 
} 
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;   
} 
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~` 
相關問題