Array.indexOf(value) - not giving proper index number in GAS

16,719

in a simple for loop :

function myFunction() {
  var sheet2 = SpreadsheetApp.getActiveSheet();
  var firstset = sheet2.getRange('B1:B10').getValues();
  for(var n in firstset){
    if(firstset[n][0]=='re'){break}
  }
  Logger.log(n);
}
Share:
16,719
Vasim
Author by

Vasim

I'm the BAD MAN :)

Updated on June 17, 2022

Comments

  • Vasim
    Vasim almost 2 years

    In GAS,

    Code.gs
    function myFunction() {
      var sheet2 = SpreadsheetApp.getActivesheet();
      var firstset = sheet2.getRange('B1:B10').getValues();
      Logger.log(firstset)
      var found = firstset.indexOf('[re]')
      Logger.log(found)
    }
    

    In Google Spreadsheet Range B1:B10,

    B1  we
    B2  we
    B3  re
    B4  ef
    B5  ge
    B6  er
    B7  fe
    B8  th
    B9  rg
    B10 pq
    

    The answer to the code should result 2, however, Log is showing -1 (not matched). Any help

    Screenshot of Logger.log