我对谷歌脚本场景有点陌生。我已经经历了很多次,我无法正确地增加我的大脑。
我创建了一个使用表单的 Google 电子表格。当有人使用表单提交设备时,它会使用发送电子邮件功能(MailApp.sendEmail)将其通过电子邮件发送给会计人员。
到目前为止,我可以完美地填充电子表格,但是当函数运行时,它只标记第一行中的“状态”变量并且不会继续通过列表,然后它通过电子邮件多次发送该 1 行。谁能看到我在这方面哪里错了?
我需要它来将数据提交到可用的下一行,然后脚本检查最后一个单元格的 EMAIL_SENT,如果它没有那个值,我需要它发送一封电子邮件,然后检查下一行数据以及(如果我需要从电子表格中手动运行脚本)。我在这里找不到类似的问题,我可以理解我在递增和循环方面做错了什么。
这是脚本:
function sendThisOut() {
// set the current spreadsheet and active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// set range data
var sRow = 2; // first top left cell of data
var cols = 9; // Num of cols
// define initial data source and get values
var dataRange = sheet.getRange(sRow,1,1,cols);
var data = dataRange.getValues();
// increment data
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// define each column
var tstamp = row[0]; // get timestamp (col A)
var acct = row[1]; // define account (col B)
var cname = row[2]; // define client name (col C)
var comment = row[3]; // define comments (col D)
var item1 = row[4]; // defines item 1 (col E)
var item2 = row[5]; // defines item 2 (col F)
var item3 = row[6]; // defines item 3 (col G)
var item4 = row[7]; // defines item 4 (col H)
var status = row[8]; // get status (emailed or no?)
// setup e-mail vars
var emailAddr = "myEmail@myDomain.com";
var subject = "ACCT:" + acct + " " + cname + " - Equipment Request";
// setup content of e-mail
var body = "Hello Accounting,\n\n" +
"Please bill for the contained items on the following account:\n\n" +
"Account: " + acct + "\n\n" +
"Client Name: " + cname + "\n\n" +
"Item 1: " + item1 + "\n\n" +
"Item 2: " + item2 + "\n\n" +
"Item 3: " + item3 + "\n\n" +
"Item 4: " + item4 + "\n\n" +
"Reason for rekey: " + comment;
// check status and if it doesn't have EMAIL_SENT, email it
if (status != "EMAIL_SENT") {
MailApp.sendEmail(emailAddr, subject, body);
// Make sure the cell is updated right away in case the script is interrupted
sheet.getRange(sRow + i, 9).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
};
};
}
谢谢你的时间!