2015-12-30 22 views
0

我在這裏度過了一段艱難的時光,我知道它不會那麼困難。我有一個for循環,它運行在我試圖編寫的Google Apps腳本中的一系列單元格值中,每次它循環時,它都會檢查以確定某個條件是否爲真。如果它是真的,那麼現在它將結果記錄到Logger.log()。用JavaScript計算循環內條件爲真的次數

我需要在Logger.log()中顯示的結果總數(在這種情況下,它將是10),我能夠想到的最好方式是每次加1 for循環中條件爲真的時間。獲得多少結果的數值應該非常簡單,但我沒有經驗,並且一直在尋找我認爲可能能夠幫助我的任何事情的小時數,而我無法弄清楚。這是我正在使用的代碼。

var currentsheet = SpreadsheetApp.getActiveSheet(); 
var values = currentsheet.getRange("A28:E").getValues(); 

for (i=0; i<values.length; i++) { 
if (values[i][3] === "Section 179 Depreciation") { 
    var date = values[i][0]; 
    var amount = values[i][1]; 
    var desc = values[i][2]; 
    var expType = values[i][3]; 
    var notes = values[i][4]; 

} 
} 

如果有人能填補我對我失蹤的東西,我將非常感激!

+4

得到一個櫃檯前,'for'它設置爲零,內部'如果'增加一個。在'for'之後,你會得到你想要的 – Tushar

+1

如果關閉不起作用,那麼在裏面放一個櫃檯?還是有另一個問題是你面臨的?它應該是非常簡單的。 –

+0

@blindProgrammer我要指出的是,我提到我沒有經驗,但有人決定編輯它,所以你無法知道。我雖然回滾了。但是,是的,我很缺乏經驗,並且自從我上次使用javascript做了任何事情之後已經有一段時間了。 – Soundfx4

回答

1
var currentsheet = SpreadsheetApp.getActiveSheet(); 
var values = currentsheet.getRange("A28:E").getValues(); 
var j=0; 
for (i=0; i<values.length; i++) { 
if (values[i][3] === "Section 179 Depreciation") { 
    var date = values[i][0]; 
    var amount = values[i][1]; 
    var desc = values[i][2]; 
    var expType = values[i][3]; 
    var notes = values[i][4]; 
    j++; 
} 
} 
alert(j); 

,讓你可以在迴路中

2

成立的情況後總價值以一個全局變量,計數器和0初始化,並增加計數器的值時,它進入if條件。它會給你多少次的條件是真的。每當你想開始計數只是初始化計數器爲0

var currentsheet = SpreadsheetApp.getActiveSheet(); 
var values = currentsheet.getRange("A28:E").getValues(); 

var counter = 0; 
for (i = 0; i < values.length; i++) { 
    if (values[i][3] === "Section 179 Depreciation") { 
     var date = values[i][0]; 
     var amount = values[i][1]; 
     var desc = values[i][2]; 
     var expType = values[i][3]; 
     var notes = values[i][4]; 
     counter++; 
    } 
} 
+0

我在這裏有點尷尬。我知道我沒有經驗,但我甚至覺得我應該意識到這一點。我知道它必須很簡單。非常感謝你。 – Soundfx4

+1

NP ..它發生了。 – Shubham

3

您可以使用Array.prototype.filter過濾值並獲取其長度:

var filtered = values.filter(function(x) { 
    return x[3] === "Section 179 Depreciation"; 
}); 
var count = filtered.length; 

或使用ES6箭頭功能:

var filtered = values.filter(x => x[3] === "Section 179 Depreciation"); 
var count = filtered.length; 

重新使用這些filtered值會比較方便,例如記錄它們:

filtered.forEach(function(x) { 
    var date = x[0]; 
    var amount = x[1]; 
    var desc = x[2]; 
    var expType = x[3]; 
    var notes = x[4]; 
    // log 
}); 

它現在遍歷數組兩次,這會影響性能,但它絕對提高了可讀性。如果性能不重要,請使用此選項。

2

如果您需要使用數據,我會將它保存到一個數組中,以便您可以使用數組方法。

var currentsheet = SpreadsheetApp.getActiveSheet(); 
 
var values = currentsheet.getRange("A28:E").getValues(); 
 
var section179 = []; 
 

 
for (i=0; i<values.length; i++) { 
 
if (values[i][3] === "Section 179 Depreciation") { 
 
    var date = values[i][0]; 
 
    var amount = values[i][1]; 
 
    var desc = values[i][2]; 
 
    var expType = values[i][3]; 
 
    var notes = values[i][4]; 
 
    
 
    section179.push({ 
 
    date: date, 
 
    amount: amount, 
 
    desc: desc, 
 
    expType: expType, 
 
    notes: notes 
 
    }); 
 
    
 
} 
 
} 
 

 
console.log(section179.length); 
 
console.log(total(section179, 'amount')); 
 

 
// example total up all the results of a number property 
 
function total(arr, prop){ 
 
    return arr.reduce(function(ret, curr){ 
 
    return ret + curr[ prop ]; 
 
    }, 0); 
 
}

1

這是Javascript For Loop

在這種情況下,我會用計數器方式如下:

var currentsheet = SpreadsheetApp.getActiveSheet(); 
var values = currentsheet.getRange("A28:E").getValues(); 

var counter = 0; // counter 
for (i=0; i<values.length; i++) { 
if (values[i][3] === "Section 179 Depreciation") { 
    var date = values[i][0]; 
    var amount = values[i][1]; 
    var desc = values[i][2]; 
    var expType = values[i][3]; 
    var notes = values[i][4]; 
    counter++; //we encrease counter every time condition is true 
} 
}