本文介绍了根据内容闪烁单元格脚本 - 如何无限期执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

function onEdit(e) {
  if (e.value == "Requested") {
    var number = 50; // Number of blink
    for (var i = 0; i <= number * 2; i++) {
      if (i % 2 == 0) {
        var color = "white";
      } else {
        var color = "red";
      }
      e.range.setBackground(color);
      SpreadsheetApp.flush();
      Utilities.sleep(1500); // Blink speed (ms)
    }
    e.range.setBackground("white") // Cell color is white after blinks were completed.
  }
}

但是由于某种原因,即使我将它设置为 50,单元格闪烁的次数也少于 10 次左右.谷歌运行 for 循环时是否有时间限制?有没有办法让它无限期,直到我将单元格值更改为其他值?

However for some reason the cell blinks less than 10 times or so even though I have set it to 50. Is there a time limit when google runs a for loop? Is there a way to make it indefinite until I change the cell value to something else?

推荐答案

向 Google 服务发出如此多的调用会占用您的每日配额.在此处详细了解配额 https://developers.google.com/apps-script/指南/服务/配额

Making so many calls to Google services will count against your daily quotas. More on quotas here https://developers.google.com/apps-script/guides/services/quotas

仅将服务调用用于真正重要的事情,例如获取和处理数据.找到另一个解决方案来突出显示编辑过的单元格.

Only use service calls for something that's really important like fetching and processing data. Find another solution to highlight the edited cell.

由于延迟,您永远无法完美渲染闪烁,但您可以将等待时间缩短至 50 毫秒.查看下面的脚本.请注意,这是非常糟糕的做法,应不惜一切代价避免.

Because of latency, you'll never get the blinking to render perfectly, but you could bring the wait time down to 50 milliseconds. Check out the script below. Be advised, this is extremely bad practice and should be avoided at all costs.

   //junk code: not for use

    var i = 0;

    var isRunning = true;

    while(isRunning) {

      if (i % 2 == 0) {
        var color = "white";
      } else {
        var color = "red";
      }

      i++;
      e.range.setBackground(color);
      SpreadsheetApp.flush();
      Utilities.sleep(30); // Blink speed (ms)
    }

尝试对多个单元执行它.您会注意到脚本将在一段时间后停止执行.那是因为 Google 限制了总执行时间,并且不允许您的脚本连续运行.如果您在脚本编辑器中转到查看"->执行脚本",您将看到类似执行失败:超出最大执行时间 [32.548 秒总运行时间]"这样的内容.

Try executing it for several cells. You'll notice that the script will stop executing after some time. That's because Google limits total execution time and will not allow your script to run continuously. If you go to View -> Execution transcript in Script Editor, you'll see something like this 'Execution failed: Exceeded maximum execution time [32.548 seconds total runtime]'.

这篇关于根据内容闪烁单元格脚本 - 如何无限期执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:15