我正在寻找有关脚本的帮助,该脚本会在单元格中满足条件时发送电子邮件。
示例表连接如下。调查回复转入标签"表格回复1,"我要发送的电子邮件的数据位于标签"所有者。"
当第一栏说"是"我希望将电子邮件正文中的K列发送到J列中的电子邮件地址。
答案 0 :(得分:0)
此功能可确保在授权发送每封电子邮件之前正确输入所有必要的数据。特别是它还检查DateSent是否为空并且SendIfYes为“是”。此外,它设置为每十分钟提供一次日志条目,并检查和设置具有正确数据的电子邮件,并且还有DateSent =''和SendIfYes ='是'。
为了让它适合您,首先您必须为它创建和基于时间的触发器。我用10分钟的触发器测试它,它工作正常。您可以设置这些触发器 在“脚本编辑器编辑/当前项目触发器”菜单中,您可以从提供的“我的工具”菜单中运行setupEmailTrigger。如果您希望终止基于时间的触发 您可以使用“我的工具”菜单中的delEmailTrigger命令执行此操作。您也可以从“我的工具”菜单中手动运行脚本。
您必须在saveFile函数中填写DataFolderID和DefaultFileName。
SendingEmail.gs:
function sendingMyEmails()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getSheetByName('EmailSetup');
var rng=sht.getDataRange();
var rngA=rng.getValues();
var s='\n' + 'Checking for Emails that need to be sent';
for(var i=2;i<rngA.length;i++)
{
var dataA={};
for(var j=0;j<rngA[1].length;j++)
{
dataA[rngA[1][j]]=rngA[i][j];
}
if(dataA.EntryDate && dataA.Name && dataA.Email && dataA.Subject && dataA.Body && dataA.SendIfYes=='Yes' && dataA.DateSent=='' && dataA.SendAs)
{
//MailApp.sendEmail(dataA.Email, dataA.Subject, dataA.Body, {replyTo:dataA.Sender});
GmailApp.sendEmail(dataA.Email, dataA.Subject, dataA.Body, {replyTo: dataA.Sender,from: dataA.SendAs});
s+='\n' + 'Email Sent: Row: ' + Number(i+1) + ' Name: ' + dataA.Name + ' Email: ' + dataA.Email + ' Subject: ' + dataA.Subject + ' EntryDate: ' + dataA.EntryDate;
sht.getRange(i+1,8).setValue(Utilities.formatDate(new Date(), 'GMT-6', "M/dd/yyyy'\n'HH:mm:ss"));
SpreadsheetApp.flush();
}
else
{
var row=Number(i+1);
s+='\n' + 'Email Not Sent: Row: ' + Number(i+1) + ' Name: ' + dataA.Name + ' Email: ' + dataA.Email + ' Subject: ' + dataA.Subject + ' EntryDate: ' + dataA.EntryDate + ' SendIfYes: ' + dataA.SendIfYes + ' Date Sent: ' + dataA.DateSent;
}
}
s+='\n';
logEntry(s);
}
function setupEmailTrigger()
{
nMinuteTrigger('sendingMyEmails');
logEntry('\n10 Minute trigger setup for sendingMyEmails.');
}
function delEmailTrigger()
{
delTriggers('sendingMyEmails');
logEntry('\nDeleted All Project Triggers for sendingMyEmails.');
}
Code.gs:
function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('sendingMyEmails','sendingMyEmails')
.addItem('setupEmailTrigger','setupEmailTrigger')
.addItem('delEmailTrigger','delEmailTrigger')
.addToUi();
}
function nDayTrigger(funcName,n)
{
var n = (typeof(n) != 'undefined')?n:1;
ScriptApp.newTrigger(funcName).timeBased().everyDays(n).create();
}
function nHourTrigger(funcName,n)
{
var n = (typeof(n) != 'undefined')?n:1;
ScriptApp.newTrigger(funcName).timeBased().everyHours(n).create();
}
function nMinuteTrigger(funcName,n)
{
var n = (typeof(n)!='undefined')?n:10;
ScriptApp.newTrigger(funcName).timeBased().everyMinutes(n).create();
}
function delTriggers(funcName)
{
if(funcName)
{
var triggers=ScriptApp.getProjectTriggers();
for(var i=0;i<triggers.length;i++)
{
if(triggers[i].getHandlerFunction()==funcName)
{
ScriptApp.deleteTrigger(triggers[i]);
}
}
}
else
{
SpreadsheetApp.getUi().alert('Invalid Parameter', 'funcName is undefined or invalid in function delTriggers in Code.gs', SpreadsheetApp.getUi().ButtonSet.OK);
}
}
function logEntry(entry,file)
{
var file = (typeof(file) != 'undefined')?file:'eventlog.txt';
var entry = (typeof(entry) != 'undefined')?entry:'No entry string provided.';
if(entry)
{
var ts = Utilities.formatDate(new Date(), "GMT-6", "yyyy-MM-dd' 'hh:mm:ss a");
var s = ts + ' - ' + entry + '\n';
saveFile(s, file, true);
}
}
function saveFile(datstr,filename,append)
{
var append = (typeof(append) !== 'undefined')? append : false;
var filename = (typeof(filename) !== 'undefined')? filename : DefaultFileName;
var datstr = (typeof(datstr) !== 'undefined')? datstr : '';
var folderID = (typeof(folderID) !== 'undefined')? folderID : DataFolderID;
var fldr = DriveApp.getFolderById(folderID);
var file = fldr.getFilesByName(filename);
var targetFound = false;
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
if(append)
{
datstr = fi.getBlob().getDataAsString() + datstr;
}
targetFound = true;
fi.setContent(datstr);
}
}
if(!targetFound)
{
var create = fldr.createFile(filename, datstr);
if(create)
{
targetFound = true;
}
}
return targetFound;
}