0

Hi I have the following code:

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.
  }
}

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Areki
  • 41
  • 1
  • 8
  • Possible duplicate of [Google Spreadsheet Script to Blink a range of Cells](https://stackoverflow.com/questions/38066294/google-spreadsheet-script-to-blink-a-range-of-cells) – Rubén Apr 25 '19 at 01:59

1 Answers1

0

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.

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)
    }

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]'.

Anton Dementiev
  • 5,451
  • 4
  • 20
  • 32
  • Ahh, thanks for clarifying. It makes sense for google to have a time out for scripts given that everything on it is server side. I guess maybe in the future they can introduce a little bit of client side functionality so menial macros like these can work in the background. I will just abandon this idea altogether and figure out a better system to highlight the cells. – Areki Jun 08 '17 at 09:10