简体   繁体   English

Google Apps电子表格脚本会随机停止?

[英]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; 我编写了一个简单的Google Apps Spreadsheet脚本,该脚本在某些行中获取详细信息并将其转换为单个列表; 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. 我最初认为它可能是以某种方式受到限制,但是随机停止点(有时在源表行6x,有时在7x,有时很好到数百)并且缺少错误消息暗示不然。

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. 一种更有效的方法是将所有数据读入2D数组,然后在两个嵌套循环中一次性写回电子表格。

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

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

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