Google Sheets Script – If Else Statement for Edited Drop-Down Selection

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I have a dropdown field on a sheet (consisting of Wheat, Wood, Stone, etc.). When that dropdown field is edited, I want it to run one of a few similar functions, based on the dropdown selection (when dropdown selection is Wheat, select from range X, for example). I have a function that seems to accurately be logging what cell the dropdown is in, the previous value, and the updated value. But now that I have an if else statement for each of the selections, it seems to give conflicting values in the log.

I think either I am not setting the value as a string properly, or I am instantiating the variables improperly so that they are one thing for parts of the method – and something else for different parts? Here is the code below, as it current is

function onEdit (evt) {

  var selection = evt.value;
  var range = evt.range;

  Logger.log("Hex Changed from: " + evt.oldValue + " to : " + selection);

    if (selection = "Brick") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else if (selection = "Gold") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else if (selection = "Sheep") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else if (selection = "Stone") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else if (selection = "Wheat") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else if (selection = "Wood") {
    Logger.log("It was " + selection);
    Logger.log("The Paste Area is from row " +[range.getRow()+2] + " and column " +[range.getColumn()-1] + " to row " + [range.getRow()+6] + " and column " + [range.getColumn()+1]);
  } else {
    Logger.log("Something went wrong");
  }
}

The problem is that it seems that when it gets to the if statements, that the 'selection' value is ALWAYS "Brick", even when the selection in the first part of the log was correctly identified as what the dropdown was edited to be (see screenshot below).

So my question is, how can I get the code to run so that when it correctly selects what the updated "selection" variable is (happening now), it does the correct if statement (not happening now)?

Script Log

Best Answer

I don't know if this is the only one error, but this is certainly one of the first things that you should fix: the equality comparison operators should be === not =.

The above because in Google Apps Script / JavaScript a single = is used to assign a value / object to a variable, the equality comparison operators are == (abstract) and === (strict).

Reference

Expressions and Operators

Related questions