简体   繁体   English

JavaScript 循环中不考虑 Google-Sheet 的第一行

[英]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++) { ... }

From Mozilla :来自Mozilla

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.

相关问题 Google App Script - Google-Sheet 行未按预期按时间戳排序 - Google App Script - Google-Sheet rows are not sorting by timestamp as expected 在Google Sheet JavaScript中添加一行 - adding a row to google sheet JavaScript 在Google工作表脚本编辑器中-无法对数组中的字符串使用替换功能 - in the google-sheet script editor - Can't use subsrting function for a string in array 通过 php 脚本将 Google-Sheet 馈送的 json 字符串转换为具有内插键默认值的数组 - converting a Google-Sheet fed json string into an array with interpolated key default values via php script 循环 Google 工作表 - 仅返回最后一行 - Loop Google sheet - returns only last row 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 用于创建innerHTML的JavaScript循环(Google Sheet) - JavaScript loop to create innerHTML (Google Sheet) 使用javascript的for循环从Google表格中提取数据? - Pull data from a google sheet with a for loop with javascript? 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script 如何使用 JavaScript 从 doGet 获取 Google 表格中的指定行? - How to obtain a specified row from a Google Sheet from doGet with JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM