Reputation: 109
I need to get array from one line in sheet. First I wrote this
let shSettings = SpreadsheetApp.getActive().getSheetByName("НАСТРОЙКИ");
let kuDeads = shSettings.getRange("A1:N1").getValues();
Next I need to get length of my array and check the value in item with my variable
for (var j = 1; j < kuDeads.length; j ++){
colJ = j + 1;
if (shSettings.getRange(1,colJ).getValue() == priorKU){
deadDays = shSettings.getRange(row,colJ).getValue();
break;
}
}
But I get the info, that the length = 1. And cycle is broken.
Upvotes: 0
Views: 54
Reputation: 624
length
gives the length of the array (number of rows).
kuDeads[0].length
gives the number of columns.
let shSettings = SpreadsheetApp.getActive().getSheetByName("НАСТРОЙКИ");
let kuDeads = shSettings.getRange("A1:N1").getValues();
for (var j = 1; j < kuDeads[0].length; j ++){
colJ = j + 1;
if (shSettings.getRange(1,colJ).getValue() == priorKU){
deadDays = shSettings.getRange(row,colJ).getValue();
break;
}
}
Upvotes: 1
Reputation: 14527
In addition to the answer of arul selvan.
To use getValue()
(and setValue()
) in a loop is not the best idea if there are more than a couple iterations. It can be painfully slow if you have dozens of hundreds cells. You need to use getVaues()
and process the 2d array in your script.
In your case it can be done about this way:
let data = shSettings.getRange("A2:N").getValues();
for (var j = 1; j < kuDeads[0].length; j++) {
if (data[0][j] == priorKU) {
deadDays = data[row-1][j-1];
break;
}
}
It will work much faster.
Upvotes: 0