在使用MailApp.sendEmail时适当递增 [英] Proper Incrementing while using MailApp.sendEmail
问题描述
我对Google脚本编写场景有点新鲜。我经历了这么多次,我无法将自己的大脑包装在正确的增量中。
I'm a bit new to the Google scripting scene. I've gone over this so many times and I can't wrap my brain around incrementing properly.
我创建了一个使用表单的Google Spreadsheet。当有人使用表单提交设备时,它将使用发送电子邮件功能(MailApp.sendEmail)将其发送给会计人员。
I've created a Google Spreadsheet that uses a form. When someone uses the form to submit equipment, it's going to use a Send Email function (MailApp.sendEmail) to e-mail it to the accounting people.
到目前为止,我可以完美地填充spreadhsheet,但是当函数运行时,它只会标记第一行中的'status'变量,并且不会继续遍历列表,然后它会多次向该行发送电子邮件。任何人都可以看到我去哪里这么错了吗?
So far, I can populate the spreadhsheet perfectly, but when the function runs, it only labels the 'status' variable in the 1st row and doesn't continue through the list, then it e-mails that 1 row several times. Can anyone see where I'm going so wrong on this?
我需要这个提交数据到下一个可用的行,然后脚本检查最后一个单元格EMAIL_SENT,如果它没有这个值,我需要它发送一封电子邮件,然后检查下一行数据(以防我需要从电子表格中手动运行脚本)。我无法在这里找到类似的问题,在这里我可以理解我在做什么错误的增量和循环。
I need this to submit the data to the next row that available, then the script checks the last cell for EMAIL_SENT and if it doesn't have that value, I need it to send an e-mail and then check the next row of data as well (in case I need to manually run the script from within the spreadsheet). I couldn't find a similar question on here where I could understand what I'm doing wrong with the incrementing and looping.
这是脚本:
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();
};
};
}
感谢您的宝贵时间!
推荐答案
因此,如上所述,我不得不一直反复这一点。最后,通过学习不同的技术并找到一些分散的线程,我最终得到了以下脚本:
- 在提交时只检查最后一行数据
- 用户是否提交了一个表格
- 电子邮件预定方
- 更新电子表格
- 手表中断
- 提供电子邮件确认/休息时间
So as mentioned above, I had to keep going over and over this. Finally, through learning a different technique and finding a few spread out threads, I ended up with the following script that: - simply checks the last row of data at time of submission - Has the user submit a form - Emails intended parties - Updates spreadsheet - Watches for breaks - Provides confirmation of email / breaks
我希望这可以帮助别人。到目前为止,我仍在编写一个脚本来扫描整个电子表格,但是这个脚本检查提交表单的最后一行,它回答了我的特定问题。当我找到我的答案时,我会尽力发布答案:
I hope this helps someone else out there. So far I'm still working on a script that will scan out the entire spreadsheet, but this script checks the last row on submission of a form, which answers my particular question. I'll try to post the answer when I find my answer to the full sheet scan:
(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!");
}
}
}
这篇关于在使用MailApp.sendEmail时适当递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!