在 for 循环中编译信息以发送电子邮件作为确认
Posted
技术标签:
【中文标题】在 for 循环中编译信息以发送电子邮件作为确认【英文标题】:Compiling information in a for loop to email as confirmation 【发布时间】:2022-01-16 15:35:57 【问题描述】:我有以下代码来运行 for 循环并发送所有包含足够信息的发票。该代码工作正常,但我想在完成脚本运行时编译的信息后向自己发送一封确认电子邮件。我需要的代码在 *********** 部分。提前致谢!
function testSendAll()
var ss = SpreadsheetApp.getActiveSpreadsheet();
var invoice = ss.getSheetByName("Invoice");
var range = invoice.getRange("B13"); // Cell of validation
const values = [...new Set(range.getDataValidation().getCriteriaValues()[0].getValues().flat())]; // Gets array of validation
var first = values[0]; // 1st cell of validation
var number = values.length - 1; // Length of validation
range.setValue(first); // Sets value to first one
for(i = 0;i < number;i++) // Loop number of names
var test = invoice.getRange("B18").getValue();
var email = invoice.getRange("B14").getValue();
var person = invoice.getRange("B13").getValue();
var amount = invoice.getRange("E3").getValue();
if (test != "")
if (email != "You have not added this contact")
// emailSpreadsheetAsPDF();
// makeHistory();
// *************************************************************
//******* Here I would like to compile person and amount for each loop ************
// ***********************************************************
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
const emailAdd = 'me@myemail.com';
// Subject of email message
const subject = 'Your Sent Invoices';
// Now the message
// **************************************************************
// ****** Here I would like to include all the gathered information from the for loop
// I would like it to read "I have sent invoices to *person1* for *amount1*,
// *person2* for *amount2*.... etc
// ********************************************************************************
const body = message;
GmailApp.sendEmail(emailAdd, subject, body,
htmlBody: htmlMessage,
);
【问题讨论】:
【参考方案1】:我相信你的目标如下。
您想在 for 循环中创建类似"I have sent invoices to *person1* for *amount1*, *person2* for *amount2*.... etc"
的电子邮件。
这样的话,下面的修改怎么样?
发件人:
for(i = 0;i < number;i++) // Loop number of names
var test = invoice.getRange("B18").getValue();
var email = invoice.getRange("B14").getValue();
var person = invoice.getRange("B13").getValue();
var amount = invoice.getRange("E3").getValue();
if (test != "")
if (email != "You have not added this contact")
// emailSpreadsheetAsPDF();
// makeHistory();
// *************************************************************
//******* Here I would like to compile person and amount for each loop ************
// ***********************************************************
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
收件人:
var message = "I have sent invoices to ";
for (i = 0; i <= number; i++)
var test = invoice.getRange("B18").getValue();
var email = invoice.getRange("B14").getValue();
var person = invoice.getRange("B13").getValue();
var amount = invoice.getRange("E3").getValue();
message += `$person for $amount`;
if (i == number) continue;
if (test != "")
if (email != "You have not added this contact")
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
else
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
if (i < number)
message += ", ";
// SpreadsheetApp.flush(); // This might be required to be used.
在这种情况下,message
是`“我已经为 amount1 发送发票给 person1,对于 amount2person2 /em>....等"``.所以它可以用作消息体。如下。
const emailAdd = 'me@myemail.com';
const subject = 'Your Sent Invoices';
const body = message;
GmailApp.sendEmail(emailAdd, subject, body);
在你的脚本中,循环中const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
和range.setValue(nextValue)
的3个脚本是一样的。所以我认为您也可以按如下方式对其进行修改。
var message = "I have sent invoices to ";
for (i = 0; i <= number; i++)
var test = invoice.getRange("B18").getValue();
var email = invoice.getRange("B14").getValue();
var person = invoice.getRange("B13").getValue();
var amount = invoice.getRange("E3").getValue();
message += `$person for $amount`;
if (i == number) continue;
if (test != "")
const nextValue = values[values.indexOf(range.getValue()) + 1] || values[0];
range.setValue(nextValue);
if (i < number)
message += ", ";
// SpreadsheetApp.flush(); // This might be required to be used.
此外,以下修改后的脚本可能会降低流程成本。请按如下方式修改您的 for 循环。
var message = "I have sent invoices to ";
var [[person], [email],,,, [test]] = invoice.getRange("B13:B18").getValues();
var amount = invoice.getRange("E3").getValue();
for (i = 0; i <= number; i++)
message += `$person for $amount`;
if (i == number) continue;
if (test != "") person = values[values.indexOf(++first)] || values[0];
if (i < number) message += ", ";
range.setValue(person);
【讨论】:
太棒了,这帮助我到达了我想要的地方并学习了一些新东西,谢谢!只是一个问题,在 person 之前的这一行中使用的字符是什么?消息 +=$person for $amount
;
@Alistair McEvoy 感谢您的回复。我很高兴你的问题得到了解决。关于您的其他问题,我使用了模板文字。你可以在here看到它。以上是关于在 for 循环中编译信息以发送电子邮件作为确认的主要内容,如果未能解决你的问题,请参考以下文章