javascriptreferencegoogle-sheetscell

How to reference a cell to the left of active cell in Google script


Need to reference a cell in an email subject.

Need to reference the cell left to the active cell i.e if cell in column B is changed, reference cell in same row in column A.

All of the code works apart from

var productname = range.getvalue("A"+range.getRow);

Would be very grateful if someone can point out as to where I am going wrong! Sure this will be very easy for someone that knows what they are doing.

function onEdit(e) {
    
      var range = e.range;
      
      if(range.getValue() < 100) {
        var productname = range.getvalue("A"+range.getRow);
        var message = "Product variant" + productname + "has dropped to " + range.getValue();
        var subject = "Stock Notification";
        var emailAddress = "email@emailaddress.com";
        MailApp.sendEmail(emailAddress, subject, message);
       
      }
    }

Solution

  • Try

    var productname = range.offset(0,-1).getValue();
    

    However, you may have to 'limit' the trigger to a certain sheet and even a certain column (col B ?)