Home > OS >  How to check for cell value?
How to check for cell value?

Time:11-16

I have a really simple script that is run when the sheet is edited (below). There is data validation on my cell c6 to allow only Yes or Cancel which once selected, causes the script to run. I would like the macro to check if the value Yes has been selected and if so, run the module CopyDownNews. My issue is that CopyDownNews runs every time irrespective of what is selected in C6. Any ideas?

function changeMade() {

var checkForNews = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6");

if (checkForNews='Yes') {
  
  CopyDownNews();

  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6").clearContent();
  
};  

}

CodePudding user response:

You must check for getValue()

var checkForNews = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6").getValue();

CodePudding user response:

Posting this for documentation purposes:

Issues:

  1. As MikeSteelson said, you have to retrieve the value of the range, not the range itself. Use Range.getValue().
  2. It's always entering the if block because you added an assignment operator (=), while you should be using a comparison operator (==, ===).

Code snippet:

function changeMade() {
  var range = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6");
  var checkForNews = range.getValue();
  if (checkForNews==='Yes') {
    CopyDownNews();
    range.clearContent();
  };  
}
  • Related