questions4bizness
questions4bizness

Reputation: 13

Change All Tab Colors Google Script

I'm trying to change all the tab colors in my google sheet with a script

I know how to do it by putting in the name of each individual sheet

var summary = ss.getSheetByName("Summary");
summary.setTabColor("f4c7c3"); // Set the color to red.

I also know how to get the name of all sheets

function getAllSheetNames(){
  var tabs = new Array();
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for(var i = 0; i < sheets.length;i++)tabs.push([sheets[i].getName()])
  
  console.log(tabs);
  return tabs

But I don't know how I can set the tab color for every element in the array

Upvotes: 1

Views: 433

Answers (1)

Wicket
Wicket

Reputation: 38422

Assuming that by "in every element in the array" you mean the tabs variable, it's an empty Array.

function setColorToAllSheets(){
  SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach( sheet => sheet.setTabColor("f4c7c3"));
}

The above function use method chaining, Array.prototype.forEach and arrow function to set the tab color to all sheets in the active spreadsheet.

Upvotes: 2

Related Questions