TipVisor
TipVisor

Reputation: 1092

how to get maximum index of array in appscript

I tried to get the values in a row into an array. For this I used the appscript in googlesheet. After checking the length of this rowtemp array, the answer is 1. But I want to find the number of children inside. And if the element in "temp" is the same as the one in "rowtemp" then you need to find its column number I used following code. enter image description here

    function rangeExa(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet4");

  var temp = ss.getRange("A11:B22").getValues();
  Logger.log(temp);
  Logger.log(temp.length);
  Logger.log(temp[3][0]);

  var rowTemp = ss.getRange("D25:O25").getValues();
  Logger.log(rowTemp);
  Logger.log(rowTemp.length);
  Logger.log(rowTemp[0][2]);
  Logger.log(rowTemp);

  for(i=0; i<=rowTemp.length; i++){
  if(temp[3][0] == rowTemp[0][i]){
    Logger.log("yessss");
  }return;
  }  
}

Upvotes: 0

Views: 469

Answers (1)

Marios
Marios

Reputation: 27390

As I explained in detail in the comment section, your goal is not clear.

However, there is a clear issue in the code an that relates to the length of rowTemp.

rowTemp is an array of a single array because it concers a single row. In other words, it has the format of [["High","Not High",..]]. Therefore, rowTemp.length will give you 1 since there is only one row in this array. If you want to iterate over the columns, you need to use rowTemp[0].length:

  for(i=0; i<rowTemp[0].length; i++){
    if(temp[3][0] == rowTemp[0][i]){
     Logger.log("yessss");
     Logger.log(i+4); // column number if there is a match
    }
  } 

The above for loop will check if 36 appears in D25:O25 and it will output yessss if it does in the Logs page.

  • Also use i<rowTemp[0].length instead of i<=rowTemp[0].length because the last index of your array is rowTemp[0].length-1 otherwise you will get undefined.

Related:

What does the range method getValues() return and setValues() accept?

Upvotes: 5

Related Questions