Dmitriy Rudakov
Dmitriy Rudakov

Reputation: 109

Get array from one line in Sheet

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

Answers (2)

arul selvan
arul selvan

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;
  }
}

Reference

Upvotes: 1

Yuri Khristich
Yuri Khristich

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

Related Questions