简体   繁体   English

Google App Script/Javascript - 无法使用 Google App Script 循环到 Google 电子表格中的列末尾

[英]Google App Script/Javascript - Not able to loop through up to the end of Column in Google Spreadsheet using Google App Script

I am trying to scrape through 3000+ data using Google Spreadsheet.我正在尝试使用 Google 电子表格抓取 3000 多个数据。 I used Google App Script to loop on every cell in Column A and arrange it on a different column.我使用 Google App Script 在 A 列中的每个单元格上循环并将其排列在不同的列上。 I am able to create a simple script that will loop through a cell, scrape the data, and insert it to the designated column.我能够创建一个简单的脚本,该脚本将循环遍历一个单元格、抓取数据并将其插入到指定的列中。 The problem is, the script that I made won't scrape or loop up until the end of the column.问题是,我制作的脚本直到列末尾都不会抓取或循环。 It can only scrape up to 1654 of the column even though I have 3000+ data on that column.即使我在该列上有 3000 多个数据,它也最多只能抓取该列的 1654 个。 The data is consist of multiple information of an individual The information is consist of Name, Day, Date, Time and description of the customer.数据由个人的多个信息组成。信息由客户的姓名、日期、日期、时间和描述组成。 Take note that 1 data is inserted into 1 cell in the column.请注意,将 1 个数据插入到列中的 1 个单元格中。 Meaning I have 3000+ cell with data in Column A.这意味着我在 A 列中有 3000 多个带有数据的单元格。

Sample Data:样本数据:

John Doe Tuesday, 06/21/2021, Bought 1 pack of Bubblegum John Doe 星期二,06/21/2021,买了 1 包泡泡糖

Trish Smith Monday, 06/21/2021, Bought 1 pack of Bubblegum Trish Smith 星期一,06/21/2021,买了 1 包泡泡糖

Karen Peralta Thursday, 06/21/2021, Bought 1 pack of Bubblegum Karen Peralta 2021 年 6 月 21 日,星期四,买了 1 包泡泡糖

Additional Question.附加问题。 Is there limitation in looping through every cell using google app script?使用谷歌应用程序脚本循环遍历每个单元格是否有限制?

function rawDataAlteration() {
  var days = new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Raw Data');
  var data = spreadsheet.getRange('A1:A').getValues();
  var filteredData = new Array();
  for (q in data) {
    if(data[q] != "") {
      filteredData.push(data[q]);
    }
  }
  for (x in filteredData) {
    if (filteredData[x] != "") {
      var convertedToArray = filteredData[x][0].split(',');
      var nametoArray = convertedToArray[0].split(' ');
      var name = new Array();
      var day = "";
      var columnNum = Math.floor(x)+1;
      var nameColumn = 'A'+ columnNum;
      var dayColumn = 'B' + columnNum;
      var dateColumn = 'C' + columnNum;
      var shiftColumn = 'D' + columnNum;
      for(let i in nametoArray){
          let found = false;
          for(let j in days){
              if(nametoArray[i] == days[j]){
                  day = days[j];
                  found = true;
              }
          }
          if(!found){
              name.push(nametoArray[i]);
          }
      }
      var columnB = spreadsheet.getRange(nameColumn).setValue(name.toString().replace(',', ' '));
      var columnD = spreadsheet.getRange(dayColumn).setValue(day);
      var columnD = spreadsheet.getRange(dateColumn).setValue(convertedToArray[1]);
      // Logger.log(filteredData);
    }
  }
}

Currently, each script execution can only last six minutes .目前,每个脚本执行只能持续6 分钟 If your script takes longer than that, it'll be interrupted.如果您的脚本需要更长的时间,它将被中断。

Your snippet shows that you're using quite a few getRange() and setValue() inside a loop, which makes the processing slower and it's not recommended.您的代码段显示您在循环内使用了相当多的getRange()setValue() ,这会使处理速度变慢,不推荐使用。

According to the best practices , a better way would be to batch the calls.根据最佳实践,更好的方法是批量调用。 In your case, that means to work with all the data using arrays in the loop and then do setValues() outside the loop.在您的情况下,这意味着在循环中使用数组处理所有数据,然后在循环外执行setValues()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM