Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetValues as lowercase from Google Apps Script

I'm writing a google apps script where I get the values from a spreadsheet..but since I need to see if there is a match with a variable, I need to do make sure the values are in lowercase.

  var sheet  = SpreadsheetApp.getActiveSpreadsheet();
  var column = sheet.getRange(A:A); 
  var values = column.getValues(); 

then I want to loop through the values to see if there is a match, like this: row=0;

  while ( values[row] && values[row][0] !== data ) {

     // do something
    row ++;
  }

My question is this..while I can make sure the variable "data" is lowercase, how can I make sure values[row] and values[row][0] are also lowercase?

like image 547
Allen Avatar asked Jan 18 '16 04:01

Allen


Video Answer


2 Answers

You can loop through the values with checking lower case item,

for (var row in values)
{
    if(values[row][0].toString().toLowerCase() !== data ) 
    {
         // do something
    }
}

this is values[row] an array, what do you mean by lower case of it?

like image 96
iJay Avatar answered Oct 17 '22 10:10

iJay


So first of all I believe that with the notation you're using .getRange would take a string like "A:A" as an argument to return the range containing all values in column A.

  • Use Logger.log()and typeof to check the type of the values being returned. values will be a two-dimensional array, with each child array containing the value for each respective cell.

  • values[row] will return an array representing the cell at the index of row. An array cannot be lowercase obviously.

  • values[row][0] will return the value in the cell. You can coerce the value to a string (to make sure it's always a string) by concatenating it with an empty string (values[row][0]+""), and then you can apply the usual built-in JS toLowerCase() method, like so: (values[row][0]+"").toLowerCase();

  • So (values[row][0]+"").toLowerCase() !== data should achieve what you want.

like image 1
cssimsek Avatar answered Oct 17 '22 12:10

cssimsek