1

I am using a function that fetches values from a range, for this example ["red", "blue", "green"]

function readDataFromRange() {
  var sheet = SpreadsheetApp.getActive();
  var range = sheet.getRange("A:A");
  var cell = range.getValues();
  
  Logger.log(cell[2]);
  Logger.log(cell.length);
  Logger.log(cell.indexOf("green"));

}

this is the result

Execution log

3:23:17 AM  Notice  Execution started
3:23:17 AM  Info    [green]
3:23:17 AM  Info    1000.0
3:23:17 AM  Info    -1.0
3:23:17 AM  Notice  Execution completed

although at index 2 it shows green if ask the index of green it shows -1 and same goes for red and blue any idea why this is happening ?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • `cell` doesn’t appear to contain `"green"`, but `[ "green" ]`. If so, and if `cell` is an array, you’d likely need to use `cell.findIndex((array) => array.includes("green"));`. Which spreadsheet API is this? Google Sheets API? – Sebastian Simon Sep 12 '21 at 00:46
  • Thank you sir, works like a charm, yes im using the google sheets API – Mahmoud Shihab Sep 13 '21 at 00:08
  • Does the previous comment solved your issue? If so, can you set it as an answer? – Kessy Sep 20 '21 at 13:59
  • It makes sense: `getRange` gets a two-dimensional section of the spreadsheet. `getValues` gets the values of this two-dimensional section. `cell[2]` is not a single cell; one dimension less is either a _row_ or a _column_. `cell` is a misnomer. `"green"` is neither a row of values nor a column of values; `[ "green" ]` (logged as `[green]`) is. – Sebastian Simon Sep 21 '21 at 03:45

0 Answers0