通过 google 行完全完成的工作表发送电子邮件
Sending out email through google sheets with row fully complete
我正在尝试修改我的代码,以便我可以根据指定行中的值发送电子邮件。
问题:多个人将在进行销售时同时使用此 sheet,并且脚本调用发送包含一组不包含 'if' 值 [=] 的行的电子邮件17=]。我想插入一行或其他内容,只发送满足所有条件的行的电子邮件。我还想在发送后保护单元格,以便它们不会被错误地编辑或删除。最后,我希望没有 google 帐户的用户能够触发脚本。
`// This constant is written in column L for rows for which an email
`// has been sent successfully.`
`var EMAIL_SENT = "EMAIL_SENT";`
`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:K2
var dataRange = sheet.getRange(2,1,3,12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
var message1 = row[2]; // third column
var message2 = row[3];
var message3 = row[4];
var message4 = row[5];
var message5 = row[6];
var message6 = row[7];
var message7 = row[8];
var message8 = row[9];
var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
var emailSent = row[12]; // 12th column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}`
可以使用 IF 和 && 在一行中完成,但维护起来会很笨重且耗时。
而是添加一个新函数来测试该行。
在 for
循环周围添加这三行:
var bool;
for (var i = 0; i < data.length; ++i) {
bool = testData(data[i])
if(bool){
并且还在函数的末尾添加一个大括号。
将下面的函数添加到您的脚本文件中。
function testData(data) {
var bool = true;
for (var x = 0; x < data.length; x++) {
if (!data[x]) {
bool = false;
break;
}
}
return bool;
}
它通过将行传递给测试函数来工作。如果任何单元格为空,它将 return 为假,然后 function sendEmails2()
将跳过该行并移至下一行。
更新完整的工作代码
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:K2
var dataRange = sheet.getRange(2,1,3,12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
if(testData(data[i])){ // Pass the row of data to the testData function (testData will return true or false)
var row = data[i];
var emailAddress = row[0]; // First column
var message = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
var message1 = row[2]; // third column
var message2 = row[3];
var message3 = row[4];
var message4 = row[5];
var message5 = row[6];
var message6 = row[7];
var message7 = row[8];
var message8 = row[9];
var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
var emailSent = row[11]; // 12th column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
}
function testData(data) { // Function to test the data
for (var x = 0; x < data.length - 1; x++) { // Loop through each cell (length -1 because we don't want to check the email sent cell as that should be empty)
if (!data[x]) { // If a cell is empty return false
return false;
}
}
return true; // If no empty cells are found return true
}
我正在尝试修改我的代码,以便我可以根据指定行中的值发送电子邮件。
问题:多个人将在进行销售时同时使用此 sheet,并且脚本调用发送包含一组不包含 'if' 值 [=] 的行的电子邮件17=]。我想插入一行或其他内容,只发送满足所有条件的行的电子邮件。我还想在发送后保护单元格,以便它们不会被错误地编辑或删除。最后,我希望没有 google 帐户的用户能够触发脚本。
`// This constant is written in column L for rows for which an email
`// has been sent successfully.`
`var EMAIL_SENT = "EMAIL_SENT";`
`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:K2
var dataRange = sheet.getRange(2,1,3,12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
var message1 = row[2]; // third column
var message2 = row[3];
var message3 = row[4];
var message4 = row[5];
var message5 = row[6];
var message6 = row[7];
var message7 = row[8];
var message8 = row[9];
var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
var emailSent = row[12]; // 12th column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}`
可以使用 IF 和 && 在一行中完成,但维护起来会很笨重且耗时。
而是添加一个新函数来测试该行。
在 for
循环周围添加这三行:
var bool;
for (var i = 0; i < data.length; ++i) {
bool = testData(data[i])
if(bool){
并且还在函数的末尾添加一个大括号。
将下面的函数添加到您的脚本文件中。
function testData(data) {
var bool = true;
for (var x = 0; x < data.length; x++) {
if (!data[x]) {
bool = false;
break;
}
}
return bool;
}
它通过将行传递给测试函数来工作。如果任何单元格为空,它将 return 为假,然后 function sendEmails2()
将跳过该行并移至下一行。
更新完整的工作代码
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:K2
var dataRange = sheet.getRange(2,1,3,12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
if(testData(data[i])){ // Pass the row of data to the testData function (testData will return true or false)
var row = data[i];
var emailAddress = row[0]; // First column
var message = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
var message1 = row[2]; // third column
var message2 = row[3];
var message3 = row[4];
var message4 = row[5];
var message5 = row[6];
var message6 = row[7];
var message7 = row[8];
var message8 = row[9];
var message9 = Utilities.formatDate(row[10], "GMT" , "MM/dd/yyyy" );
var emailSent = row[11]; // 12th column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, "\nSale date: " + message + "\nCustomer: " + message1 + "\nJob: " + message2 + "\nLender: " + message3 + "\nAged inventory: " + message4 + "\nreplacing CAN: " + message5 + "\nChange option: " + message6 + "\nSource: " + message7 + "\nRealtor: " + message8 + "\nClosing: " + message9);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
}
function testData(data) { // Function to test the data
for (var x = 0; x < data.length - 1; x++) { // Loop through each cell (length -1 because we don't want to check the email sent cell as that should be empty)
if (!data[x]) { // If a cell is empty return false
return false;
}
}
return true; // If no empty cells are found return true
}