Google Script Performance Slow Down

廉价感情. 提交于 2021-02-08 11:47:18

问题


I'm new to Google script.

Anyone can give some advice how to improve the coding performance? If the numOfEmail variable is quite a huge number then the performance will slow down.

for (var i = 0; i < numOfEmail; i++)
  {
  var messages=threads[i].getMessages();  
  for (var j = 0; j < messages.length; j++) 
    {                   
    sheet.getRange("A"+(lastEntry+i)).setValue(messages[j].getId());
    sheet.getRange("B"+(lastEntry+i)).setValue(messages[j].getDate());
    sheet.getRange("C"+(lastEntry+i)).setValue(messages[j].getFrom());
    sheet.getRange("D"+(lastEntry+i)).setValue(messages[j].getSubject());
    sheet.getRange("E"+(lastEntry+i)).setValue(messages[j].getTo());
    sheet.getRange("F"+(lastEntry+i)).setValue(messages[j].getCc());
    sheet.getRange("G"+(lastEntry+i)).setValue(messages[j].getBcc());                  

    if(i/numOfEmail*100-oldPercentage>4)
      {
      oldPercentage=i/numOfEmail*100;
      sheet.toast(i/numOfEmail*100+"% completed", "In Progress", 3);
      }
    } 
  }

I did ask google and i found a answer how to speed up the script but i have no idea how to modified the code. Please kindly advice.

Actually I would like to try export my gmail to spreadsheet. The sample code that i using is from this link. Current sample code is just able to export 200 email but i change it to 1000 because i have around 500++ email in my gmail acc. When i try run the code it take quite long to run the script and never end running..seem like program is hanging some where in the code. I wondering why. And when every time the script update around 5-10 row data will took at least 20-30sec.


回答1:


Rule 1 from the best practices you referenced is to "minimize calls to services". That means you should move as much functionality as possible into regular javascript, and make calls to Google services less frequently. Anything you're doing in a loop is the prime target for this kind of optimization. For example:

sheet.getRange("A"+(lastEntry+i)).setValue(messages[j].getId());
sheet.getRange("B"+(lastEntry+i)).setValue(messages[j].getDate());
sheet.getRange("C"+(lastEntry+i)).setValue(messages[j].getFrom());
sheet.getRange("D"+(lastEntry+i)).setValue(messages[j].getSubject());
sheet.getRange("E"+(lastEntry+i)).setValue(messages[j].getTo());
sheet.getRange("F"+(lastEntry+i)).setValue(messages[j].getCc());
sheet.getRange("G"+(lastEntry+i)).setValue(messages[j].getBcc()); 

becomes

var values = [[messages[j].getId(),
              messages[j].getDate(),
              messages[j].getFrom(),
              messages[j].getSubject(),
              messages[j].getTo(),
              messages[j].getCc(),
              messages[j].getBcc()]]; 

sheet.getRange("A"+(lastEntry+i)+":G"+(lastEntry+i)).setValues(values); 

That changes 12 service calls per row to just 2.



来源:https://stackoverflow.com/questions/19120211/google-script-performance-slow-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!