简体   繁体   English

超过最大执行时间(Google表格脚本)

[英]Exceeded Maximum Execution Time (Google Sheet Script)

I'm using a script to forward a long list of emails (under one label) to a new account. 我正在使用脚本将一长串电子邮件(在一个标签下)转发到新帐户。 However, I'm hitting a exceeded maximum execution time error not even quarter way through. 但是,我遇到了超出最大执行时间的错误,甚至四分之一也没有。 I was wondering if there was a workaround, maybe a possibility to store the variables on the sheet and resume after a set time? 我想知道是否有解决方法,也许可以将变量存储在工作表上并在设置的时间后恢复? I'm not too well versed in coding so any help on this would be appreciated. 我不太熟悉编码,因此对此有所帮助。

function forwardMail() {
  var data = SpreadsheetApp.getActiveSheet().getRange("A2:B11").getValues();
  for (i in data) {
    var row = data[i];
    var name = row[0].toString();
    var email = row[1].toString();
    var label = GmailApp.getUserLabelByName(name);
    if (label && (email != "")) {
      var threads = label.getThreads();
      for (var x in threads) {
        var messages = threads[x].getMessages();
        for (var y in messages) {
          var subject = messages[y].getSubject();
          messages[y].forward(email, {
            subject: subject});
        }
        threads[x].removeLabel(label);
      }
    }
  }
}

This script should process one line in your spreadsheet during execution. 该脚本应在执行过程中处理电子表格中的一行。 On the following execution it will run the next line and so on. 在接下来的执行中,它将运行下一行,依此类推。 It could still use some work, but it's a good place start and hopefully conveys the idea I have for you to circumvent the maximum execution time. 它仍然可以使用一些工作,但这是一个很好的起点,并希望传达我的想法,以帮助您规避最大执行时间。

The idea here is you set a value in column C to indicate that a record has been processed already. 这里的想法是您在C列中设置一个值,以指示记录已被处理。 When the script runs again, it then continues from the next record. 当脚本再次运行时,它将从下一条记录继续。 Still needs some functionally to stop it from attempting to run blank rows. 仍然需要一些功能来阻止它尝试运行空白行。 But I am sure you can work that in. 但我相信您可以解决这个问题。

Once you got those minor issues sorted out you can put this on a trigger to run every 1 or 5 minutes. 解决了这些小问题后,您可以将其触发,以每1或5分钟运行一次。 (Depending on how long execution time is.) (取决于执行时间有多长。)

This is how I have beaten the execution time in the past. 这就是我过去击败执行时间的方式。 As always, hope this helps. 一如既往,希望这会有所帮助。

function forwardMail() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

var rowsToProcess = sheet.getRange("C2:C").getValues();
var rowCounter = rowsToProcess.filter(String).length +1;

for (var index = rowCounter; index = rowCounter +1; index++){
var data = sheet.getRange(index, 1, 1, 2).getValues();
var name = data[0].toString();
var email = data[1].toString();

var label = GmailApp.getUserLabelByName(name);
if (label && (email != "")) {
var threads = label.getThreads();

for (var x in threads) {
var messages = threads[x].getMessages();

for (var y in messages) {
var subject = messages[y].getSubject();
messages[y].forward(email, {subject: subject});
} //End of Y for loop

threads[x].removeLabel(label);
} //End of X for loop
} //End of IF statement
} //End of INDEX for loop

sheet.getRange(index, 3, 1, 1).setValue("Mails Processed");
} //End of Function

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

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