[英]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);
}
}
}
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.