【问题标题】:Auto Send Email When Checkbox is Checked - Google Sheet选中复选框时自动发送电子邮件 - Google 表格
【发布时间】:2020-09-21 10:21:39
【问题描述】:

我尝试了Auto Email Function for Google Sheets Mobile 的脚本,第一次就成功了。当我尝试更改变量时,它不会自动发送电子邮件,除非我再次运行脚本,否则它将更新响应表,然后清除复选框。请帮我解决一下这个。请在下面查看我的代码。

 function EmailNotification(e) {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getSheetByName('Course_AutoEmail'); //source sheet
     var columnb = sheet.getRange('I:I'); //Column with check boxes
     var columnbvalue = columnb.getValues();
     var notifysheet = ss.getSheetByName('Responses'); //destination sheet
     var data = [];
     var rownum = [];

     //Condition check in B:B (or check box column); If true copy the same row to data array

     for (let i = 0; i < columnbvalue.length; i++) {
       if (columnbvalue[i][0] === true) {
     
         var columnb2 = sheet.getRange('I747:I'); //row started in row 747
         columnb2.setValue('false');
         data.push.apply(data, sheet.getRange(i + 1, 1, 1, 20).getValues());

      //Copy matched ROW numbers to rownum

         rownum.push(i);

      //Copy data array to destination sheet

         notifysheet.getRange(notifysheet.getLastRow() + 1, 1, data.length, data[0].length).setValues(data);

         var activeRow = notifysheet.getLastRow();
         var name = notifysheet.getRange(activeRow, 1).getDisplayValue(); // The number is the column number in the destination "responses" sheet that you want to include in the email
         var employeeID = notifysheet.getRange(activeRow, 2).getDisplayValue();
         var position = notifysheet.getRange(activeRow, 3).getDisplayValue();
         var team = notifysheet.getRange(activeRow, 4).getDisplayValue();
         var date = notifysheet.getRange(activeRow, 10).getDisplayValue();
         var rec1 = notifysheet.getRange(activeRow, 19).getDisplayValue();
         var rec2 = notifysheet.getRange(activeRow, 20).getDisplayValue();
         var email = rec1 + "," + rec2;
         var subject = name + ': 201 Completion';
      //Body of the email message, using HTML and the variables from above

         var message =
           '<br><div style="margin-left:10px;">Hi, </div>' +
           '<br><div style="margin-left:10px;">Good day!</div>' +
           '<br><div style="margin-left:10px;"><b>' + name + '</b> has completed the course. </div>' +
        
           '<br><div style="margin-left:20px;"><h3 style="text-decoration: underline; color: #f36f21">Employee Details: </h3></div>' +
           '<div style="margin-left:25px;">Name: <b>' +
           name +
           '</b></div>' +
           '<div style="margin-left:25px;">Position: <b>' +
        position +
           '</b></div>' +
           '<div style="margin-left:25px;">Team: <b>' +
        team +
           '</b></div>' +
           '<div style="margin-left:25px;">Completion Date: <b>' +
        date +
           '</b></div>' +
           '<br><br><div style="margin-left:10px;">Let me know if you have any questions. </div>' +
           '<br><div style="margin-left:10px;">Thanks! </div>';

         MailApp.sendEmail(email, subject, '', {
           htmlBody: message,
           name: 'Updates', 
         });
    }
  }
}

 

【问题讨论】:

  • 触发器:EmailNotification > 从电子表格 > 编辑时
  • 您是否在简单或可安装的onEdit 触发器上设置了触发器?在工作表中执行编辑后,从 Apps 脚本编辑器转到 View-&gt;Executions 并查看您的功能是否已触发以及是否给您任何错误。

标签: google-apps-script google-sheets google-sheets-formula


【解决方案1】:

我读了你的代码。
如果您不想在电子表格中取消选中选中的复选框。
所以你必须删除 `columnb2.setValue('false');` 行代码。
此行未选中复选框。 其他问题的解决方案:http://one7techlab.com

【讨论】:

  • 我试过了,但我的问题是它不会自动发送电子邮件,也不会更新回复表。我必须重新运行应用程序脚本才能发送选中行的电子邮件。
  • 您必须在该函数上设置基于时间的触发器。
  • 您可以在每 10 分钟设置一次触发器,如果​​您有空或不在工作表中。
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 2019-05-02
  • 2015-08-10
  • 2017-05-29
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多