Google脚本性能下降 [英] Google Script Performance Slow Down

查看:47
本文介绍了Google脚本性能下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google脚本的新手.

I'm new to Google script.

任何人都可以提出一些建议,以提高编码性能?如果numOfEmail变量很大,则性能会降低.

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.

实际上,我想尝试将我的gmail导出到电子表格.我使用的示例代码来自以下链接.当前的示例代码只能导出200封电子邮件,但我将其更改为1000封,因为我的gmail帐户中包含大约500 ++封电子邮件.当我尝试运行代码时,要花很长时间才能运行脚本,并且永远不会停止运行..好像程序在代码中挂了一些地方.我想知道为什么.而且,每次脚本更新大约5-10行数据时,至少要花费20-30秒.

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是最小化对服务的调用".这意味着您应将尽可能多的功能转移到常规javascript中,并减少对Google服务的调用.您在循环中所做的任何事情都是此类优化的主要目标.例如:

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()); 

成为

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); 

这会将每行12个服务调用更改为仅2个.

That changes 12 service calls per row to just 2.

这篇关于Google脚本性能下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆