Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to temporarily change color of active cell using Apps Script

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading