本文介绍了根据Google表格中的数据使用Apps脚本模板化电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉脚本,所以我希望有人能帮助我创建一个。



我试图为可发送电子邮件的工作表创建脚本当一个自定义菜单被按下。



这是一个文件。


此外,只有在列(如列I)具有特定批准之类的值



电子邮件内容应该如下所示:

电子邮件应发送至E栏和F列。



脚本还必须更新电子表格以避免重复的电子邮件。

解决方案

这是

以下是代码:

  function sendEmails(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var startRow = 2; //第一行数据处理
var lastRow = sheet.getSheetByName('Journal')。getLastRow(); //最后一行包含内容

var rangeEmailSent = sheet.getRangeByName('Journal!EmailSent');
var dataEmailSent = rangeEmailSent.getValues();
var dataRequestedBy = sheet.getRangeByName('Journal!RequestedBy')。getValues();
var dataRequestType = sheet.getRangeByName('Journal!RequestType')。getValues();
var dataRefNo = sheet.getRangeByName('Journal!RefNo').getValues();
var dataStatus = sheet.getRangeByName('Journal!Status').getValues();
var dataToEmail = sheet.getRangeByName('Journal!ToEmail').getValues();
var dataSupComment = sheet.getRangeByName('Journal!SupervisorComment')。getValues();

var subjectTemplate = sheet.getRangeByName('SubjectTemplate1')。getValue();
var bodyTemplate = sheet.getRangeByName('BodyTemplate1')。getValue();
var msgSubject;
var msgBody;

for(var i =(startRow-1); i
//发送电子邮件, 并非空白,如果状态不为空
if(!(dataEmailSent [i] =='Yes')&&!(dataStatus [i] =='')){

msgSubject = subjectTemplate.replace('$ REF $',dataRefNo [i]);

msgBody = bodyTemplate
.replace('$ REQUESTED_BY $',dataRequestedBy [i])
.replace('$ REQUEST_TYPE $',dataRequestType [i])
.replace('$ EMAIL $',dataToEmail [i])
.replace('$ STATUS $',dataStatus [i])
.replace('$ SupervisorComment $',dataSupComment [i] );
// Logger.log(msgSubject);
// Logger.log(msgBody);

MailApp.sendEmail(dataToEmail [i],msgSubject,msgBody);
//将发送电子邮件更改为是
rangeEmailSent.getCell(i + 1,1).setValue('Yes'); //注意:getCell(1,1)引用第一个单元格
}
}
}


I'm new to scripting so I hope someone can help me create one.

I'm trying to create a script for a sheet that can send an email when a custom menu is pressed.

Here's a file to work on.https://docs.google.com/spreadsheets/d/1Ea-3eZoclHrAkZLwRmWWFbmbnn4dESNWvK_6pn1DCbE/edit?usp=sharing

Also, it should only send it if a column (ex. Column I) has a specific Value like 'Approved'

Email content should look like:

Email should be sent to Column E and F.

The script must also update the spreadsheet to avoid duplicate emails.

解决方案

Here is the working example

Here is the code:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var startRow = 2;  // First row of data to process
  var lastRow = sheet.getSheetByName('Journal').getLastRow();  // Last row with content

  var rangeEmailSent     = sheet.getRangeByName('Journal!EmailSent');
  var dataEmailSent      = rangeEmailSent.getValues();
  var dataRequestedBy    = sheet.getRangeByName('Journal!RequestedBy').getValues();
  var dataRequestType    = sheet.getRangeByName('Journal!RequestType').getValues();
  var dataRefNo          = sheet.getRangeByName('Journal!RefNo')      .getValues();
  var dataStatus         = sheet.getRangeByName('Journal!Status')     .getValues();
  var dataToEmail        = sheet.getRangeByName('Journal!ToEmail')    .getValues();
  var dataSupComment     = sheet.getRangeByName('Journal!SupervisorComment').getValues();

  var subjectTemplate = sheet.getRangeByName('SubjectTemplate1').getValue();
  var bodyTemplate =  sheet.getRangeByName('BodyTemplate1').getValue();
  var msgSubject;
  var msgBody;

  for (var i = (startRow-1); i <= (lastRow-1); i++) {

    // send e-mail if "Email Sent" is not blank and if "Status" is not empty
    if ( !(dataEmailSent[i]=='Yes') && !(dataStatus[i] =='')) {

      msgSubject = subjectTemplate.replace('$REF$', dataRefNo[i]);

      msgBody = bodyTemplate
        .replace('$REQUESTED_BY$', dataRequestedBy[i])
        .replace('$REQUEST_TYPE$', dataRequestType[i])
        .replace('$EMAIL$', dataToEmail[i])
        .replace('$STATUS$', dataStatus[i])
        .replace('$SupervisorComment$', dataSupComment[i]);
      // Logger.log(msgSubject);
      // Logger.log(msgBody);

      MailApp.sendEmail(dataToEmail[i], msgSubject, msgBody);
      // Change "Email sent" to "Yes"
      rangeEmailSent.getCell(i+1,1).setValue('Yes'); // note: getCell(1,1) refers to the 1st cell
    } 
  } 
}

这篇关于根据Google表格中的数据使用Apps脚本模板化电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:53