Reputation: 3
I'm doing a loop in google sheets script, i'm trying to find a row in the "Inventar" sheet that has the same value found in the sheet "Fisa client" and then change the value of a cell in that row but it appears that it does not enter the second for. I'm new to this so i don't know what's the problem.
function adaugabucati1(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var ss=sheet.getSheetByName('Fisa client');
var ss2=sheet.getSheetByName('Inventar');
var data = ss.getRange("g9:l18").getValues();
var data1=ss2.getDataRange().getValue();
for(var i=0;i<data.length;i++){
if(!data[i][0] == ""){
var cod = ss.getRange(i+9,8).getValue();
var buc = ss.getRange(i+9,10).getValue();
for(var y=0;y<data1.length;y++){
if(data1[y][5] == cod){
var bucati = data[y][7]-buc;
ss2.getRange(y+1,8).setValue(bucati);
}
}
}
}
}
Upvotes: 0
Views: 61
Reputation: 7949
The problem is in this line:
var data1=ss2.getDataRange().getValue();
getValue()
returns only one value - the value of the top-left cell in the range.
When the if
statement following the second loop evaluates data1[y][5]
, it returns "undefined" and the rest of the second loop is bypassed.
You need to change line#6 to:
var data1=ss2.getDataRange().getValues();
Note getValue()
- > getValues()
Upvotes: 2