Google表格脚本(自动电子邮件功能)

时间:2018-06-08 17:54:41

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

Google表格脚本

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

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

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我为与此类似的脚本创建了一个脚本,除了它没有复选框。您可以通过添加.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
});

}
}
}
}

答案 1 :(得分:0)

尝试一下:

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'});
        
      }
    }
  }
}