Home > Mobile >  In Google Apps script I am trying to retrieve a value from a cell in a Google sheet but the value is
In Google Apps script I am trying to retrieve a value from a cell in a Google sheet but the value is

Time:01-22

The aim of the following function is to retrieve a value from a cell and call a different function based on the value. When I run it, it always runs the default function and it looks like it's not returning the value into the variable (survey) as this is shown as undefined when I debug the function, and the log value is null:

function hideOrShow() {
  var spreadsheet = SpreadsheetApp.getActive();
  var survey = spreadsheet.getSheetByName('Store View 2022').getRange(3,1).getvalue;
  Logger.log (survey);
  if (survey='Delivery') {
    showCancellationColumns();
    }
  else if (survey='C&C') {
    showCancellationColumns();
    }
  else { hideCancellationColumns();}

};

Thanks Annette

CodePudding user response:

It's just an error in the "getValue()" method

try this:

function hideOrShow() {
var spreadsheet = SpreadsheetApp.getActive();
var survey = spreadsheet.getSheetByName('Store View 2022').getRange(3,1).getValue();
Logger.log (survey);
if (survey='Delivery') {
  showCancellationColumns();
  }
else if (survey='C&C') {
  showCancellationColumns();
  }
else { hideCancellationColumns();}

};

CodePudding user response:

There are 2 things that need to be changed in your code:

.getValue() (note the capital V--JavaScript variables are case sensitive) is a function, so it needs parentheses:

var survey = spreadsheet.getSheetByName('Store View 2022').getRange(3,1).getValue();

That should solve the issue of survey being undefined.

When we test for equality (in an if statement, for example), we use double or triple equals signs:

if (survey=='Delivery')

and

else if (survey=='C&C') 

As written with a single equals (assignment operator), if (survey='Delivery') will assign the value 'Delivery' to survey.

  •  Tags:  
  • Related