简体   繁体   English

Google电子邮件提取脚本-超过最大执行时间(无论如何)

[英]Google email extraction script - Exceeded maximum execution time (No matter what)

I am using the below script to extract email addresses from gmail based upon some search criteria and output them to a google spreadsheet. 我正在使用以下脚本根据一些搜索条件从gmail提取电子邮件地址,并将它们输出到Google电子表格。 Functionally, the script works and does what I want it do. 从功能上讲,该脚本可以工作,并且可以完成我想要的工作。

However, I am constantly getting "Exceeded maximum execution time" when I run the script as the maximum execution time for gmail scripts appears to be five minutes. 但是,当我运行脚本时,由于gmail脚本的最大执行时间似乎是五分钟,所以我不断收到“超过最大执行时间”。 I have tested this with a smaller label in gmail with a handful of emails and the script runs successfully and outputs emails as expected. 我已经在gmail中使用了较小的标签(带有少量电子邮件)进行了测试,脚本成功运行并按预期输出电子邮件。 However when I attempt to extract anything in larger batches with more emails the script cannot finish. 但是,当我尝试以更多的电子邮件批量提取任何内容时,脚本无法完成。

This script is cobled from other stuff I found on the web. 该脚本与我在网络上发现的其他内容有关。 I have attempted to amend this time out issue by adding for loops in a try block with the exception being caught and sent to sleep so that the script could pause execution and not exceed the time limit, however this did not work. 我试图通过在try块中添加for循环来修正此超时问题,但该异常会被捕获并发送到睡眠状态,以便脚本可以暂停执行并且不超过时间限制,但是这不起作用。 I have also tried other methods of sending the script to sleep to prevent time out from occuring but these where unsccessful. 我还尝试了其他方法来将脚本发送到睡眠状态,以防止发生超时,但是这些方法均成功。

Could someone help me in preventing the time out from occurring or else use some more efficient way of searching through email threads to grab the emails out? 有人可以帮助我防止超时发生,还是可以使用更有效的方式搜索电子邮件线程来抓取电子邮件?

Edit: I have amended the code with the suggestions added, however it still cannot complete without reaching execution time limit. 编辑:我修改了代码,添加了建议,但是,如果没有达到执行时间限制,它仍然无法完成。 Any ideas why the script is not pausing? 有什么想法为什么脚本不会暂停? I have also attempted to search just one message using GmailApp.search(search, 0, 1) however the script will not complete when I search my Inbox. 我还尝试使用GmailApp.search(search,0,1)仅搜索一封邮件,但是当我搜索“收件箱”时脚本不会完成。

function extractEmailAddresses() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var userInputSheet = ss.getSheets()[0];

  var labelName = userInputSheet.getRange("B2").getValue();
  var keyword = userInputSheet.getRange("C2").getValue();

  var sheetName = "Label: " + labelName;
  var sheet = ss.getSheetByName (sheetName) || ss.insertSheet (sheetName, ss.getSheets().length);
  sheet.clear();

  var messageData = [];

  var search = "label:" + label + "is:unread " + keyword;

  // Process 50 Gmail threads in a batch to prevent script execution errors
  var threads = GmailApp.search(search, 1, 1);      

  var messages, from, email, subject, mailDate;

  try {

    for (var x=0; x<threads.length; x++) {

      var message = threads[x].getMessages()[0]; //Get message for thread
      from = message.getFrom();
      mailDate = message.getDate();
      from = from.match(/\S+@\S+\.\S+/g);   

      if ( from.length ) {            

        email = from[0];
        email = email.replace(">", "");
        email = email.replace("<", "");       

        //push emails to array
        messageData.push ([email, mailDate]);
      }
    }
  }

  catch (e) {
    //Pause script to prevent exceeded timeout error
    Logger.log(e.toString());
    Utilities.sleep(5000);
  }

  //Adding our emails to the spreadsheet
  sheet.getRange (1, 1, messageData.length, 2).setValues (messageData);

}

Two quick ideas, I've not looked at it in detail and don't know the API well: 我有两个简单的想法,我没有详细研究它,并且不太了解API:

  1. Don't call getMessages twice: 不要两次调用getMessages

    Replace: 更换:

     from = threads[x].getMessages()[0].getFrom(); mailDate = threads[x].getMessages()[0].getDate(); 

    with: 有:

     var message = threads[x].getMessages()[0]; from = message.getFrom(); mailDate = message.getDate(); 
  2. Avoid using setValue on each iteration: Single cell updates to the spreadsheet will be slow, as each time will involve communicating with Sheets infrastructure and committing a value. 避免在每次迭代中使用setValue :对电子表格的单个单元格更新将很慢,因为每次都会涉及与Sheets基础结构进行通信并提交值。 Instead, build up a bigger array of cell values to change and set it all at once using setValues(Object[][]) 相反,使用setValues(Object[][])建立更大的单元格数组以进行更改并一次将其全部设置

Those are just thoughts from a quick glance, sorry. 抱歉,这些只是一眼的想法。

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

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