pm2588
pm2588

Reputation: 81

Google Sheets: Change colour/highlight active spreadsheet tab

Is there a way to automatically change the tab colour of the active sheet within Google Sheets, instead of just the sheet name changing to green?

Upvotes: 1

Views: 1611

Answers (1)

ross
ross

Reputation: 2774

Just wrote this based on the .setTabColor() script example supplied by google in the Class Sheet Documentation.

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var numSheets = ss.getNumSheets();
  var active = ss.getActiveSheet();
  var tabCol = active.getTabColor();

  if (tabCol == null) {
    //loop through all sheets and clear tab colour.
    for (var i = 0; i < numSheets; i++) {
      sheets[i].setTabColor(null);
    }
    active.setTabColor("ff0000"); //change ff0000 to whatever colour hex value you'd like. 
  }
}

Basically this script will check if the sheet you're currently editing has a color assigned, if it does then it'll do nothing, if not then it'll give it a color. I've included a basic for loop to go through all of the sheets and clear their colors so that only the sheet you're currently editing will have a color assigned.

Upvotes: 1

Related Questions