Google Apps脚本 - 如果cell ='yes',则使用onEdit运行脚本

时间:2017-01-20 01:04:43

标签: javascript function google-apps-script google-sheets

我无法理解Google应用脚本中的onEdit()函数。

我有一张Google工作表,其中包含一个主要标签和4个单独的标签,如果用户在任何单个工作表的“发送电子邮件”列中选择“是”,我需要它发送电子邮件。我有一个完美的功能,在调试器中,我会在这里粘贴它:(我知道它可能会被简化/减少:)

代码:

 function triggers() {
 //Hold all values of selected sheet
 var zy = holdingData.values;

 for (var j = 0; j < zy.length; ++j) {
     //Get the UI functions and store them in a variable, used for prompts later in the script
     var ui = SpreadsheetApp.getUi();

     if (zy[j][9] === 'Yes') {

         //Pull values for later use
         holdingData.selVals.push([zy[j][0], zy[j][1], zy[j][2], zy[j][3], zy[j][4], zy[j][5], zy[j][7], zy[j][23], zy[j][24], zy[j][25]]);

         //Pull Teacher email from Master column B
         holdingData.teacherEmail.push(holdingData.selVal[j][7]);

         //Assign email based on contents of column X
         if (holdingData.selVals[j][8].toLowerCase() === "miranda") {
             holdingData.counselorEmail.push("FakeEmail1@Madeup.com");
         } //.....Other cases after this, cut for length

         //Assign subject line for specified individual
         holdingData.subject.push("Referral: " + holdingData.selVals[j][0] + " " + holdingData.selVals[j][1]);

         //Store columns C,D, and E in 1 variable string for each student
         holdingData.actionsTaken.push(holdingData.selVals[j][2] + ", " + holdingData.selVals[j][3] + ", " + holdingData.selVals[j][4]);

         //Assign Email message contents for specified individual sheet
         holdingData.message.push("The referral submitted for " + holdingData.selVals[j][0] + " " + holdingData.selVals[j][1] + " has been completed. The following action(s) were taken: " + holdingData.actionsTaken[j] + ", Please email the member of the discipline team who handled the referral if you have any questions or concerns, at: " + holdingData.counselorEmail[j] + ".");

         //Prompt user to confirm they would like to send the emails
         var response = ui.prompt('Are you sure you would like to send emails to the selected staff memebers?', ui.ButtonSet.YES_NO);
         if (response === 'YES') {
             //Send Message to Referring teacher
             MailApp.sendEmail(holdingData.teacherEmailMir[j], holdingData.subjectMir, holdingData.messageMir);

             //Send Message to Counselor assigned to student
             MailApp.sendEmail(holdingData.counselorEmailMir[j], holdingData.subjectMir, holdingData.messageMir);
         } else {return;}
     }
 }

}

我剪了一些长度,但我想做的核心就在那里。如果我将整个事物包装在onEdit()中,它就不起作用。如果我将OnEdit()应用为一个简单的触发器(我相信),它将在每个时间激活此脚本,任何工作表都被编辑。我尝试过其他一些东西,但没有运气。我得到了什么? :)

1 个答案:

答案 0 :(得分:3)

因为您尝试发送包含简单onEdit()触发器的电子邮件,所以没有任何反应。在受限制的文档here中,它甚至作为示例给出了

  • 他们无法访问需要授权的服务。例如,一个简单的触发器无法发送电子邮件,因为Gmail服务需要授权,但简单的触发器可以使用匿名的语言服务翻译短语。