简体   繁体   English

如何使用 App Script 编辑 Google Sheet 的 email

[英]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.我正在尝试编写一个 function 根据单元格的值向 Google 表格的编辑发送电子邮件。 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.我能够成功设置向所有者发送电子邮件的 function。 The problem is that the "owner" aka the one creating the forms, will JUST be making the forms and not running the event themselves.问题是“所有者”,也就是创建 forms 的人,只会制作 forms 而不会自己运行事件。

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).使用getEditors的问题在于它返回一个数组(因为它们可能不止一个编辑器)。 Do you have any guidance on how I would structure this for it to email the editors?您对我如何将其构建给 email 编辑有任何指导吗?

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.根据我从您的问题中了解到的情况,您的问题是您想向作为编辑的编辑发送一个 email 一个包含所有editors的数组。 Also, noting that GmailApp.sendEmail() can only send emails to a single recipient at a time.另外,请注意GmailApp.sendEmail()一次只能向单个收件人发送电子邮件。

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.下面我建议对您的脚本进行更改,迭代所有编辑器,然后能够向每个编辑器发送 email。 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() .另请注意, getEditors返回用户 object,而不是电子邮件,因此必须使用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.让我知道您是否需要其他任何内容,或者您是否不理解某些内容。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 工作表中的Google App脚本分组数据以电子邮件形式发送 - Google App Script Grouping data in sheet to email 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data Google App脚本:触发电子邮件并使用CC - Google App Script: Trigger for email and using CC 如何通过谷歌表格脚本 email 一系列数据? - How to email a range of data via google sheet script? Google App Script + Google Sheet + HTML 模态对话框输入表单 - 获取值并构建 email - Google App Script + Google Sheet + HTML modal dialog input form - Get values and build email 如何使用gs从谷歌表格发送带有格式化表格的电子邮件? - How to send an email with a formatted table from google sheet using gs? 如何使用带有JavaScript的Google表格发送带有PHP的电子邮件? - How to send an email with PHP, using a Google sheet with JavaScript? Google App脚本触发电子邮件 - Google App Script to trigger email 使用 Google 表格和 App 脚本时如何在 Email 中获得正确的格式 - How to get the correct formatting in an Email when using Google Sheets and App Script 如何使用Google App脚本从电子邮件中提取内联文件/图像? - How to lift inline files/images from an email using Google App Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM