有时不会发送电子邮件(如果必须运行第一个子例程,则会跳过第二个子例程)
Emails sometimes not being sent (if first subroutine has to run, second subroutine is skipped)
我的代码在发送电子邮件方面运行良好。它应该向提交表单的用户发送电子邮件。
问题在于,对于某些行,当它需要运行自动审批子例程(代码注释中的 SECTION A)时,它将无法运行下一个子例程(代码注释中的 SECTION B,这是应该实际发送电子邮件的子例程)。
如果我运行整个脚本两次,那么它会发送电子邮件。此外,如果您手动批准行,则会发送电子邮件。但是,如果脚本批准该行,则不会在同一次运行中发送电子邮件。
谷歌企业应用脚本代码:
function sendEmails2() {
//trying to adopt code from this tutorial https://developers.google.com/apps-script/articles/sending_emails
//-believe there is an issue with where I am declaring getRange to work-
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var lastRow = 40; // Last row of data to process
// Fetch the range of cells A1:B10002
var dataRange = sheet.getRange(startRow, 1, lastRow, 11)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var partTimeApproved = row[0]
var userID = row[2]
var emailAddress = row[7]; // sometimes this shit is like "invalid email address found" but it sends it anyway
var isEmailSent = row[9];
var partTimeApprovedAutomatically = row[10]
var cc = 'list of CCs'
//this next section should automatically approve rows submitted by approved users
// SECTION A
if (userID == "user 1" || userID == "user 2" || userID == "user 3") {
if (partTimeApproved != 'Y') {
sheet.getRange(startRow+parseInt(i, 10), 1).setValue('Y')
sheet.getRange(startRow+parseInt(i, 10), 11).setValue('Y')
}
{}
}
{}
// SECTION B
var nowIsPartTimeApproved = row[0] // I wrote this in a vain attempt at getting the system to process section A and B at the same time
if (nowIsPartTimeApproved == 'Y') { // prevents sending emails to non-approved rows.
if (isEmailSent == '') { // Prevents sending duplicates and sending emails to non-approved rows
var options = {
cc: cc
}
var subject = "Email Subject Test";
var message = "Email body test"; // Standard string OR sheet.getRange(1,13) ;
MailApp.sendEmail(emailAddress, subject, message, options)
sheet.getRange(startRow + parseInt(i, 10), 10).setValue('emailSent'); //trying this from Stackoverflow's epascarello
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
}
有什么想法吗?
在 A 部分中,"partTimeApproved"和 B 部分中的"nowIsPartTimeApproved"指的是相同的单元格值。因此,它只会满足 if 条件之一。因此,如果执行 A 部分,它将不会执行 B 部分。
您必须在部分 A 中将单元格值(行 [0])更改为"Y",或者在部分 A 本身中将变量"nowIsPartTimeApproved"的值设置为"Y"。
希望对您有所帮助!
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 多个JavaScript子例程的实时DOM显示
- 使用jQuery从TR父级中选择第一个TD子级
- 使用异步子例程对一组函数进行计时
- 一个按钮上的多操作与javascript例程
- 获得第一个N<tr>其具有<td>作为使用jQuery的子级
- BST中的节点计数实现了伪经典的countNodes实现,没有通过Javascript的内部子例程
- 另一个最接近的子线程:查找与选择器匹配的元素的最近祖先
- 从 setInterval 回调调用子例程会引发类型错误