[英]First row of Google-Sheet is not being considered in JavaScript Loop
I am trying to send an automatic email on a reoccurring basis.我正在尝试定期发送自动电子邮件。 My Google-App-script is working fine with the exception that it does not email the 1st email, ie first row.我的 Google-App-script 工作正常,只是它不会通过电子邮件发送第一封电子邮件,即第一行。 It starts the looping process of emails from the second row on.它从第二行开始电子邮件的循环过程。 How do I tweak this issue in my code?如何在我的代码中调整这个问题?
function sendemail() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var secondSheet = spreadSheet[1];
var dataRange = secondSheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i < data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[0];
var message = 'Test Email';
MailApp.sendEmail(emailAddress, message);
})(i);
}
}
It only emails 'email2' and 'email3' but not to 'email1' in this case.在这种情况下,它只发送电子邮件“email2”和“email3”,而不发送“email1”。 How do I get it to send emails to all emails?我如何让它向所有电子邮件发送电子邮件?
Your for-loop should start with zero.你的 for 循环应该从零开始。 When you start with 1, you skip the first email in the array.当您从 1 开始时,您将跳过数组中的第一封电子邮件。
for (var i = 0; i < data.length; i++) { ... }
JavaScript arrays are zero-indexed. JavaScript 数组是零索引的。 The first element of an array is at index 0, and the last element is at the index value equal to the value of the array's length property minus 1.数组的第一个元素的索引为 0,最后一个元素的索引值等于数组的长度属性值减去 1。
getValues()
returns an array, so you need to be careful to use the correct index. getValues()
返回一个数组,因此您需要小心使用正确的索引。 This is different from the range indexes used by Google, but is noted in the documentation.这与 Google 使用的范围索引不同,但在文档中有所说明。
Remember that while a range index starts at 1, 1, the JavaScript array is indexed from [0][0].请记住,虽然范围索引从 1, 1 开始,但 JavaScript 数组从 [0][0] 开始索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.