简体   繁体   中英

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. I used Google App Script to loop on every cell in Column A and arrange it on a different column. 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. 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. Meaning I have 3000+ cell with data in Column A.

Sample Data:

John Doe Tuesday, 06/21/2021, Bought 1 pack of Bubblegum

Trish Smith Monday, 06/21/2021, Bought 1 pack of Bubblegum

Karen Peralta Thursday, 06/21/2021, Bought 1 pack of Bubblegum

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 . 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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