0

I would like an automatic notification to be sent to several people when I entered 'Yes' in column C. I wish this would apply to Sheet1 only, not to the entire file. I have this code

function myFunction () {

   var sh0 = SpreadsheetApp.getActiveSpreadsheet (). getSheetByName ("Sheet1");

   var editedCell = sh0.getActiveRange (). getColumnIndex ();

if (sh0 = "Sheet1" && editedCell == 3) {


var recipients = "xx@xx.com";
  var subject = "xxx";

  var body = "xxx"

    MailApp.sendEmail (recipients, subject, body)
  }
}

However, this works for the entire file, not just Sheet1. What is wrong? I would like to additionally add information from the twin cell from column A to the content of the e-mail.

1
0

Test the name of the active sheet against a string literal like 'Sheet1' or a regular expression that matches that name and any others you want.

To get the value in the "twin cell" in column A, use sheet.getDisplayValue(), like this:

function moot(e) {
  if (!e) {
    throw new Error('Please do not run the script in the script editor window. '
      + 'It should run through an installable trigger.');
  }
  const sheet = SpreadsheetApp.getActiveSheet();
  if (e.value !== 'Yes'
    || e.range.columnStart !== 3
    || !sheet.getName().match(/^(Sheet1)$/i)) {
    return;
  }
  const twinCell = sheet.getRange(e.range.rowStart, 1);
  const recipients = 'mggl@example.com';
  const subject = 'Today is a good day to look at documentation';
  const body = 'Here goes: ' + twinCell.getDisplayValue();
  MailApp.sendEmail(recipients, subject, body);
}

Some of the best resources for learning Google Apps Script include the Beginner's Guide, the New Apps Script Editor guide, the Fundamentals of Apps Script with Google Sheets codelab, the Extending Google Sheets pagejavascript.infoMozilla Developer Network and Apps Script at Stack Overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.