Home > OS >  How to temporarily change color of active cell using Apps Script
How to temporarily change color of active cell using Apps Script

Time:09-10

So this is an interesting one...in most simple terms - I have a script that jumps me to a specific cell (based on matches in both the column and row - script below for context):

function Jump(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var s = ss.getSheetByName("In-Office"); // change to sheet containing dates
      var r = s.getRange("ah1").getValue(); //change A1 to cell containing =match formula
      var row = s.getRange("ai1").getValue();
      s.setActiveSelection(s.getRange( r   row )); 
}

I have this running from a custom menu, where the user can jump to the cell that contains today's date.

I can use conditional formatting to highlight that cell, but there's been an idea I've wanted to execute in other projects and it feels now would be the time to try it out.

Basically, when the cell jumps, I want the COLOR to change for 10 seconds, then reset itself.

I've tried doing this with macros, but the color change is instant, so you never see it.

I tried implementing a "wait period" similar to how you would while showing a TOAST alert, but wasn't able to figure it out.

My other thought is.. setting up a conditional formatting rule through the script, delaying it for 10 seconds, then deleting it. Seems like a lot to just color the activated cell for a specific amount of seconds.

Anyone ever try something like this before? Essentially, I want to flash a different color for the active cell - I think it could be applicable to many other projects as well.

CodePudding user response:

Here's one way to handle that:

const targetCell = 'B12'; // I'm being lazy here, but hoping you get the idea
const range = s.setActiveSelection(targetCell);
const bgColor = range.getBackground();
range.setBackground('red');
SpreadsheetApp.flush();
Utilities.sleep(10000);
range.setBackground(bgColor);
SpreadsheetApp.flush();
  • Related