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

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.


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();}

};