简体   繁体   English

谷歌脚本性能下降

[英]Google Script Performance Slow Down

I'm new to Google script. 我是Google脚本的新手。

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. 如果numOfEmail变量是一个非常大的数字,那么性能会变慢。

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. 其实我想尝试将我的Gmail导出到电子表格。 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. 目前的示例代码只能导出200封电子邮件,但我将其更改为1000,因为我的gmail acc中有大约500封++电子邮件。 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. 并且当每次脚本更新大约5-10行数据时至少需要20-30秒。

Rule 1 from the best practices you referenced is to "minimize calls to services". 您引用的最佳实践中的规则1是“最小化对服务的调用”。 That means you should move as much functionality as possible into regular javascript, and make calls to Google services less frequently. 这意味着您应该将尽可能多的功能移动到常规JavaScript中,并且不那么频繁地调用Google服务。 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. 这会将每行12个服务调用更改为2。

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

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