简体   繁体   English

Google 应用程序脚本 6 分钟执行时间限制

[英]Google apps script 6 minute execution time limit

I am copying data from google sheets into a mysql table via a stored procedure.我正在通过存储过程将数据从谷歌表复制到 mysql 表中。 This sheet has some 3000 odd records.这张表有大约 3000 条记录。 After copying some 2000 odd records I hit the 6 minute execution time limit.在复制了 2000 多条记录后,我达到了 6 分钟的执行时间限制。 Is there a work around to this 6 minute execution time limit.是否有解决此 6 分钟执行时间限制的方法。

function myfunction() { 
  var colA;
  var colB; 
  var colC; 
  var colD;
  var colE;  

  var mysqldb = Jdbc.getConnection("jdbc:mysql;dbipaddress","user","pa$$word");
  var sql = mysqldb.createStatement();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1'); 
  var data = sheet.getDataRange().getValues();  

  for (var i = 1; i < data.length; i++) {
  colA = data[i][0];
  colB = data[i][1]; 
  colC = data[i][2]; 
  colD = data[i][3]; 
  colE = data[i][4];  

  var query = "{call [dbo].[sp_copygsheets](?,?,?,?,?)}";
  sql = mysqldb.prepareCall(query);
  sql.setString(1, colA);
  sql.setString(2, colB); 
  sql.setString(3, colC); 
  sql.setString(4, colD);
  sql.setString(5, colE); 
  sql.executeUpdate();
  }

  sql.close();
  mysqldb.close();
}

Without access to a SQL db to test, and with little experience of working with JDBC in Google AppScripts, I'm going to have to make some presumptions无法访问要测试的 SQL 数据库,并且在 Google AppScripts 中使用 JDBC 的经验很少,我将不得不做出一些假设

I will presume the biggest portion of the execution time in the for loop我将假定for循环中执行时间的最大部分

for (var i = 1; i < data.length; i++) {
...
}

If so, have you tried limiting the loop to something that finishes in the 6 minute time limit and saving last value of i as a user property ?如果是这样,您是否尝试将循环限制为在 6 分钟时限内完成并将i最后一个值保存为用户属性 Then you can run the script multiple times until the the script is marked as completed (as, say, another user property).然后您可以多次运行该脚本,直到该脚本被标记为已完成(例如,另一个用户属性)。

Eg Something like:例如:

var startVal = PropertiesService.getUserProperties().getProperty("lastLoop");
    for (var i = startVal; i < numLoops; i++) {
    ...
    }
var newProp = {myStartVal: startVal+numLoops};
PropertiesService.getUserProperties().setProperties(newProp);

Then set the script on a time trigger and have it run until numLoops equals data.length , which can be tested prior to the execution of the for loop.然后在时间触发器上设置脚本并让它运行直到numLoops等于data.length ,这可以在执行 for 循环之前进行测试。

Not a full answer, but I hope it puts you in the right direction.不是一个完整的答案,但我希望它能让你朝着正确的方向前进。

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

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