我有按脚本自动发送电子邮件的功能。但我只想在单元格C:“完成”时自动发送 并且,如果单元格12中有一个[K]数字1,则在发送电子邮件时会自动写入数字1。但是自动触发总是发送电子邮件。
表的外观如下: https://docs.google.com/spreadsheets/d/1jgAdvV1UcAnh9qw3QQU9q_8MVSIyZmrukhOVxz3CJUA/edit#gid=0
// This constant is written in column L for rows for which an email
// has been sent successfully.
var EMAIL_SENT = '1';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3); //
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var text = sheet.getRange("D2").getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var message = row [2]; // Third column
var emailSent = row[3]; // Fourth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = 'Text is'+ text +'';
MailApp.sendEmail('example@gmail.com', subject, message);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);//sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT)
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
要发送电子邮件,它将仅在单元格D中搜索更改为“完成”的状态
答案 0 :(得分:0)
要检查C列中的值是否为“完成”,请在if
语句中加上条件state == "done"
:
...
var emailSent = row[11];// twelfth column
var state = row[3]; // Fourth column
if (state == "done"&&emailSent!=EMAIL_SENT) {
var subject = 'Upozornění změna stavu na '+ text +'';
MailApp.sendEmail('example@gmail.com', subject, message);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
...
还请在评论中加入卡斯珀提供的更正:
var EMAIL_SENT = 1;
和var dataRange = sheet.getRange(startRow, 1, numRows, 12);
此外,请记住,Apps脚本可以检测到last row and column,这将使您可以更优雅地重写代码:
var numRows=sheet.getLastRow()-startRow+1;
var startColumn=1;
var numColumns=sheet.getLastColumn()-startColumn+1
var dataRange = sheet.getRange(startRow, startColumn, numRows, numColumns);
答案 1 :(得分:0)
您似乎并没有花太多精力来准备这个问题。
var EMAIL_SENT = '1';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 1; //This should be changed...perhaps to sheet.getLastRow()-startRow+1
var dataRange = sheet.getRange(startRow, 1, numRows, 3); //
var data=dataRange.getValues();
var text=sheet.getRange("D2").getValues();//This should be getValue() and column D in your sheet is State so this is probably wrong
for (var i=0;i<data.length;++i) {
var row = data[i];
var message = row [2];//This is your data insert column so this is not your message
var emailSent = row[3];//This is not column12 as it is in your spreadsheet so that's wrong
if (emailSent != EMAIL_SENT) { //this does not work as it stands now
var subject = 'Text is'+ text +''; //This text is not defined on your spreadsheet
MailApp.sendEmail('example@gmail.com', subject, message);//There is no subject nor any intelligent message
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
}
}
}