简体   繁体   中英

How to email editors of a Google Sheet using App Script

I am trying to write a function that emails the editors of a Google sheet based on the value of a cell. I am running into issues with making this happen and would love some pointers. For some context- the sheet is a back end of a form and we are asking if the person filling out the form needs any special accommodations prior to our event.

I was able to successfully set up a function that emails the owner. The problem is that the "owner" aka the one creating the forms, will JUST be making the forms and not running the event themselves.

They are adding the people managing the event as editors. So I want the script to email the editors when an attendee needs special accommodations.

This is what I have for emailing the owner:

function accommodateNotify() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Form Responses 1');
  var ownerEmail = ss.getOwner().getEmail();
  var startingRow = 6;
  Logger.log(ownerEmail);
  var name = ss.getName();
  Logger.log(name);
  var url = ss.getUrl();
  Logger.log(url);
    
  var lastRow = getLastNonEmptyRow(sheet.getRange(1, 83, sheet.getLastRow(), 1));
  Logger.log(lastRow);
  var needsAccommodation = sheet.getRange(lastRow, 83).getValue();
  Logger.log(needsAccommodation);
  var requiredAccommodation = sheet.getRange(lastRow, 85).getValue();
     
  if (needsAccommodation == "Yes") {
    var message = "An attendee for " + name + " " + " has stated they need accommodations. Please review: " +url;
    var subject = "Accommodation Request Alert";
    Logger.log("sending email...\n" + "  subject: " + subject + "\n  message: " + message);
    GmailApp.sendEmail(ownerEmail, subject, message);
  }
}

The problem with using the getEditors is that it returns an array (due to their possibly being more than one editor). Do you have any guidance on how I would structure this for it to email the editors?

I managed to put this together, but it's emailing the editors twice for some reason...

function accommodateNotify() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Form Responses 1');
  var editors = ss.getEditors();
  for (var i = 0; i < editors.length; i++) {
    Logger.log(editors[i].getEmail());
    //var ownerEmail = ss.getOwner().getEmail();
    var startingRow = 2;
    var name = ss.getName();
    Logger.log(name);
    var url = ss.getUrl();
    Logger.log(url);
    var lastRow = getLastNonEmptyRow(sheet.getRange(1, 29, sheet.getLastRow(), 1));
    Logger.log(lastRow);
    var needsAccommodation = sheet.getRange(lastRow, 29).getValue();
    Logger.log(needsAccommodation);
    var requiredAccommodation = sheet.getRange(lastRow, 85).getValue();

    if (needsAccommodation == "Yes") {
      var message = "A student for " + name + " " + " has stated they need accommodations. Please review: " +url;
      var subject = "Accommodation Request Alert";
      Logger.log("sending email...\n" + "  subject: " + subject + "\n  message: " + message);
      GmailApp.sendEmail(editors, subject, message);
    }
  }
}

Solution

From what I understood from your question your problem is that you want to send an email to the editors being editors an array containing all the editors. Also, noting that GmailApp.sendEmail() can only send emails to a single recipient at a time.

Below I have propossed a change in your script iterating over all the editors to then be able to send an email to each one of them. The code has self-explanatory comments.

Also note that getEditors returns an User object, not the emails and therefore these must be obtained using getEmail() .

 if (needsAccommodation == "Yes") { var message = "A student for " + name + " " + " has stated they need accommodations. Please review: " +url; var subject = "Accommodation Request Alert"; Logger.log("sending email...\n" + " subject: " + subject + "\n message: " + message); // iterate over all the editors that are in the array for(i=0;i<editors.length;i++){ // send an email with the same message and subject to each of the editors GmailApp.sendEmail(editors[i].getEmail(), subject, message); } }

I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM