简体   繁体   中英

Google Apps Spreadsheet Scripts Randomly Stopping?

I have written a simple Google Apps Spreadsheet script that takes details in some rows and converts them to a single list; all quite straightforward. The issue is that the script keeps stopping at random points, and the loop never finishes - there is no error message, and the message at the top of the script editor still indicates it is running, when it has clearly stopped.

I initially thought maybe it was being throttled somehow, but the random stopping points (sometimes at source sheet row 6x, sometimes at 7x, sometimes well into the hundreds) and the lack of an error message suggests otherwise.

Can anyone assist me in working out why this is happening? Is it because I am running it from the script editor window? Are there any other reasons (network issues, maybe) that an apps script could stop running, and if so, can it be run in background or similar to prevent this?

Thanks!

function convertTrimsToList(){
  var ss = SpreadsheetApp.openById("[REDACTED]");
  var sheet = ss.getSheets()[0];
  var newss = SpreadsheetApp.create("MMT List");
  var newsheet = newss.getSheets()[0];

x = 1; z = 1; i = 1; q = 1; while (sheet.getRange(x,1).getValue() != "") { sheet.getRange(1,6).setValue("Processing row " + x); y = 5; while (sheet.getRange(x,y).getValue() != "") { sheet.getRange(1,7).setValue("Processing row " + y); if (x != 1) { if (y == 5 && sheet.getRange(x,1).getValue() != sheet.getRange((x-1),1).getValue()) { i++ } } newsheet.getRange(z,1).setValue(i); newsheet.getRange(z,2).setValue(q); newsheet.getRange(z,3).setValue(sheet.getRange(x,y).getValue()); z++ y++ } if (x != 1) { if (sheet.getRange(x,3).getValue() != sheet.getRange((x-1),3).getValue()) { q++ } } else { q++ } x++ } }

Your code is rather inefficient. Every call made to a service is expensive. This is probably why your code stops abruptly. A more efficient way would be to read all data into a 2D array and then after your two nested loops write back to the spreadsheet in one go.

事实证明,这与此处发生的问题相同: 在Google Apps脚本中超过最长执行时间 - 不确定为什么我没有收到错误消息,但似乎是同样的问题,所以我重构了我的代码纠正它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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