【问题标题】:Script for Google sheets (auto email function)Google 表格脚本(自动电子邮件功能)
【发布时间】:2018-06-08 17:54:41
【问题描述】:

Google 表格脚本

当“A”列标记为“已完成”时,我需要生成自动电子邮件回复。将其发送到的电子邮件地址在“I”列中,电子邮件的主题需要是“H”列中的数据,而电子邮件的正文将是所有已发送电子邮件的通用。这都是特定于每一行的。

我有多个脚本正在运行,用于隐藏行等,但没有这么复杂。

所有数据都从 Google 表单发送到电子表格,并且需要包含所有行

任何帮助将不胜感激。

【问题讨论】:

  • 您在实施中遇到的具体问题是什么?我们不是来为你写一切的,你需要尝试一些东西。查看 Apps 脚本电子表格服务参考以获取代码示例和类方法。

标签: email google-apps-script google-sheets triggers


【解决方案1】:

我为类似的东西创建了一个脚本,除了它运行在复选框之外。您可以通过添加 .getdisplayvalue 轻松编辑此代码以使电子邮件收件人变量指向列中的值

我如何使用它基本上只是作为脚本触发器,所以我不会让它替换您拥有的“进度”列,而是为他们添加一列以检查他们何时完成编辑行。

旁注:此脚本在单击警报上的“是的,我想发送电子邮件”后故意将复选框设置回默认值,否则脚本将继续运行。

如果没有看到您的 Google 表格或当前代码/插入此代码,我根本无法帮助您实施。让我知道这是否有帮助。

/*

 A function to:
 
* After clicking a checkbox, auto prompt a yes/no box
* Upon "yes", copies that row from the source sheet to a destination sheet
* send an e-mail based on that new row's data
 
 I reccomend protecting the response sheet and hiding it, as well as protecting the check box column to give edit access to only those you want to be able to send e-mails.
 
 NOTES:
 
 *It is important that the headers on your source sheet match your destination sheet.

* You have to run the script from the editor first to accept permissions for the emailapp and driveapp

* you need to set up a project trigger (edit/current project triggers) for the source sheet, on spreadsheet, on edit.

*The email will come from the owner of the google sheet and owner of the script project. (the person who reviews and accepts permissions)
 MAKE SURE you are the owner of the sheet, that you are putting the code into the editor, and that you're okay with the e-mail coming from and replying to your email address.


*/


function EmailNotification(e) {

var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Assets'); //source sheet
var columnb = sheet.getRange('B:B'); //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 (i=0; i<columnbvalue.length;i++) {

if (columnbvalue[i] == 'true') {

var columnb2 = sheet.getRange('B2:B');
columnb2.setValue('false');

// What you want to say in the pop up alert

var response = ui.alert('Hold Up!\n Are you sure you want to send an email to finance for this asset change?', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {

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 assetname = 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 owner = notifysheet.getRange(activeRow, 3).getDisplayValue();
var serial = notifysheet.getRange(activeRow, 9).getDisplayValue();
var podate = notifysheet.getRange(activeRow, 6).getDisplayValue();

var email = 'theiremail@gmail.com' //The email address in which you want to send the email notification to
var subject = 'Asset has changed department ownership!'

//Body of the email message, using HTML and the variables from above

var message =
'<br><br><div style="margin-left:40px;">Heads Up!</div>'
+'<br><br><div style="margin-left:40px;">An asset has changed department ownership.</div>'
+'<br><br><div style="margin-left:40px;"><h3 style="text-decoration: underline;">Asset Name:'+ assetname +'</h3></div>'
+'<div style="margin-left:40px;">New Departmet Owner: '+ owner +'</div><br>'
+'<div style="margin-left:40px;">Serial: '+ serial +'</div>'
+'<div style="margin-left:40px;">Purchase Date: '+ podate +'</div>'
+ '<br><br><div style="margin-left:40px;">ヽ(⌐■_■)ノ♪♬</div>';

MailApp.sendEmail(
email,
subject,
"",
{
htmlBody: message,
name: 'Sadie Stevens', //The name you want to email coming from. This will be in bold, while your e-mail address will be small in italics
});

}
}
}
}

【讨论】:

    【解决方案2】:

    试试这个:

    function EmailNotification(e) {
      var ui=SpreadsheetApp.getUi();
      var ss=SpreadsheetApp.getActiveSpreadsheet();
      var sheet=ss.getSheetByName('Assets');
      var columnb=sheet.getRange(1,2,sheet.getLastRow());
      var columnbvalue=columnb.getValues();
      var notifysheet=ss.getSheetByName('Responses');
      var data=[];
      var rownum=[];
      for (var i=0;i<columnbvalue.length;i++) {
        if (columnbvalue[i] == 'true') {
          var columnb2=sheet.getRange(2,2,sheet.getLastRow()-1);
          columnb2.setValue('false');      
          var response=ui.alert('Hold Up!\n Are you sure you want to send an email to finance for this asset change?', ui.ButtonSet.YES_NO);
          if (response==ui.Button.YES) {
            data.push(sheet.getRange(i+1,1,1,20).getValues());
            rownum.push(i);
            notifysheet.getRange(notifysheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
            var activeRow=notifysheet.getLastRow();
            var assetname=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 owner=notifysheet.getRange(activeRow, 3).getDisplayValue();
            var serial=notifysheet.getRange(activeRow, 9).getDisplayValue();
            var podate=notifysheet.getRange(activeRow, 6).getDisplayValue();
            var email='theiremail@gmail.com' //The email address in which you want to send the email notification to
            var subject='Asset has changed department ownership!'
            var message =
                '<br><br><div style="margin-left:40px;">Heads Up!</div>'
            +'<br><br><div style="margin-left:40px;">An asset has changed department ownership.</div>'
            +'<br><br><div style="margin-left:40px;"><h3 style="text-decoration: underline;">Asset Name:'+ assetname +'</h3></div>'
            +'<div style="margin-left:40px;">New Departmet Owner: '+ owner +'</div><br>'
            +'<div style="margin-left:40px;">Serial: '+ serial +'</div>'
            +'<div style="margin-left:40px;">Purchase Date: '+ podate +'</div>'
            + '<br><br><div style="margin-left:40px;">ヽ(⌐■_■)ノ♪♬</div>';
            
            MailApp.sendEmail(email,subject,"",{htmlBody: message,name: 'Sadie Stevens'});
            
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多