Home > Software design >  Values from a Range not coming up as an array
Values from a Range not coming up as an array

Time:06-24

I am unable to obtain an array from a getRange.getValues call to some data on a sheet, i.e. no brackets []. If I just create an array in the code I am OK. I am trying to use indexOf to locate a specific date. In my real code the date to match is also plucked from the sheet. In the following test code the vars are global and not shown here.

function testArray() {
  var rowTwoDates = playgroundSheet.getRange(2, 59, 1, 5).getValues();
  var strippedRowTwoDates = rowTwoDates[0].map(stripTime);
  Logger.log('array:  '   strippedRowTwoDates);
  Logger.log('indexOf:  '   strippedRowTwoDates[0].indexOf("06/09/2022"));
  var scores = [10, 20, 30, 10, 40, 20];
  Logger.log(scores); // 2
  Logger.log(scores.indexOf(30)); // 2
}
9:38:23 AM  Notice  Execution started
9:38:24 AM  Info    array:  06/07/2022,06/08/2022,06/09/2022,06/10/2022,06/11/2022
9:38:24 AM  Info    indexOf:  -1
9:38:24 AM  Info    [10.0, 20.0, 30.0, 10.0, 40.0, 20.0]
9:38:24 AM  Info    2.0
9:38:24 AM  Notice  Execution completed

CodePudding user response:

You're only checking indexOf for the first value in the array here:

Logger.log('indexOf: ' strippedRowTwoDates[0].indexOf("06/09/2022"));

I think you want to do this:

Logger.log('indexOf: ' strippedRowTwoDates.indexOf("06/09/2022"));

  • Related