我对Google脚本场景有点新鲜。我已经这么多次了,我不能把我的大脑包裹起来正确地增加。
我创建了一个使用表单的Google电子表格。当有人使用表单提交设备时,它将使用发送电子邮件功能(MailApp.sendEmail)将其通过电子邮件发送给会计人员。
到目前为止,我可以完美地填充spreadhsheet,但是当函数运行时,它只标记第一行中的'status'变量并且不会继续通过列表,然后它会多次发送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();
};
};
}
谢谢你的时间!
答案 0 :(得分:1)
我在看
sheet.getRange(sRow + i, 9).setValue(EMAIL_SENT);
不应该是(观察双引号)
sheet.getRange(sRow + i, 9).setValue("EMAIL_SENT");
答案 1 :(得分:1)
它只标记第一行中的'status'变量,而不是继续通过列表,
您只影响第一行的原因是:
中的第三个参数var dataRange = sheet.getRange(sRow,1, 1 ,cols);
应指定数据集中的行数,并且您可以看到它是1(一),这导致dataRange中只有一行。尝试以下操作,看看是否有帮助:
// set range data
var sRow = 2; // first top left cell of data
var cols = 9; // Num of cols
var rows = sheet.getLastRow() - sRow + 1
// define initial data source and get values
var dataRange = sheet.getRange(sRow,1,rows,cols);
var data = dataRange.getValues();
然后它会多次发送1行电子邮件。
不确定这一点,除非在测试期间您可能设置了多个触发器?在上述更改后尝试使用,如果仍有问题,请回发。
答案 2 :(得分:0)
如上所述,我不得不一遍又一遍。最后,通过学习不同的技术并找到一些展开的线程,我最终得到了以下脚本: - 只需在提交时检查最后一行数据 - 用户是否提交了表格 - 通过电子邮件发送电子邮件 - 更新电子表格 - 手表休息 - 提供电子邮件/休息的确认
我希望这有助于其他人。到目前为止,我仍然在编写一个扫描整个电子表格的脚本,但是这个脚本会检查提交表单的最后一行,这会回答我的特定问题。当我找到完整工作表扫描的答案时,我会尝试发布答案:
(PS:可能会更顺利地将其传入Google Scripter。我添加了很多评论,以便让自己和任何阅读此内容的人了解正在发生的事情)
function other() {
// set the current sheet
var sheet = SpreadsheetApp.getActiveSheet();
// set the last row and column
var lrow = sheet.getLastRow();
var lcol = sheet.getLastColumn();
// set the major row range and data values
var rowRng = sheet.getRange(lrow, 1, 1, lcol);
var rowData = rowRng.getValues()[0];
// setup redundancy checks for status and email
// data status, email status and submitter are added to column headers AFTER form creation
var statrng = sheet.getRange(lrow, 8); // set range of status
var stat = statrng.getValue(); // get value of status in col h
var emsrng = sheet.getRange(lrow, 9); // set range of emailed status
var ems = emsrng.getValue(); // get value of emailed status in col i
// get current submitters data and set ranges for it, then apply data to sheet
var suberng = sheet.getRange(lrow, 10); // set range of submitters email
var sube = Session.getActiveUser().getEmail(); // get submitters email
suberng.setValue(sube); // set subs email to sheet in col j
// setup the per cell data
var tstamp = rowData[0]; // col a: set timestamp (created when forms created)
var cname = rowData[1]; // col b: set the client name
var acct = rowData[2]; // col c: set the account number cell
var it1 = rowData[3]; // col d: set item 1
var it2 = rowData[4]; // col e: set item 2
var it3 = rowData[5]; // col f: set item 3
var it4 = rowData[6]; // col g: set item 4
// setup email vars
var em = "myEmail@myDomain.com"; // email that data needs to go too
var sub = "ACCT: " +acct+ " - " +cname+ "Request"; // subject in email
var body = "Hello Accounting,\n\n"
+ "Please order the following:\n\n"
+ "Item 1: " +it1
+ "\nItem 2: " +it2
+ "\nItem 3: " +it3
+ "\nItem 4: " +it4
+ "\n\nPlease send us the invoicing once complete."
+ "\n\nSubmitted by\n"
+ sube + " : " + tstamp;
// Now that all variables have been set, run checks and send the email as well
// as a confirmation email. If script doesn't complete, a failure notice is sent if possible
// as long as there's a timestamp and submitted status is empty, run if statement
if (tstamp !== "" && stat == "") {
statrng.setValue("Submitted"); // update status field since we've gotten this far
var statu = statrng.getValue(); // make sure update took for status
if (statu == "Submitted") {
if (ems == "") { // no need to continue if email has already been sent, so check
MailApp.sendEmail(em, sub, body); // send to accounting people
emsrng.setValue("Emailed"); // update status that email was successfully sent
// let submitter know it was a success
MailApp.sendEmail(sube,"ACCT: " +acct+ " - Submission Successful","Your Submission was Successful!");
}
} else {
// if form failed, let submitter know it was not a success
MailApp.sendEmail(sube,"ACCT: " +acct+ " - Submission Successful","Your Submission was Successful!");
}
}
}