I have a Google worksheet with over 50 sheets. Each sheet tab is labeled with the name of the person responsible for making changes in that sheet.
I would like a script that makes all sheet tabs red until A2 is no longer blank. When A2 contains text, the sheet tab will change to green. If the text in A2 is erased making the cell blank again, the sheet tab should change back to red.
I am new to script and have tried a few snippets found on the internet but they all look for specific text in A2 to change the tab color. I need the tab color to change based on the cell being blank or nonblank.
Thank you for your help!
function greenAndRed() {
const ss = SpreadsheetApp.getActive();
ss.getSheets().forEach(sh => {
if(sh.getRange("A2").isBlank()) {
sh.setTabColor("#ff0000");//red
} else {
sh.setTabColor("#00ff00");//green
}
});
}
I believe your goal is as follows.
I thought that in your situation, when all sheets are scanned every run, the process cost will become high. So, in this answer, how about the following flow?
Here, 2 functions are used. The sample script is as follows.
Please copy and paste the following script to the script editor of Google Spreadsheet. And, in this sample, in order to reduce the process cost of the script, Sheets API is used. So, please enable Sheets API at Advanced Google services .
In this sample script, you can set the excluded sheets. If you want to exclude the sheets for setting the tab color, please set the sheet names you want to exclude to the variable of excludeSheetnames
. If you don't want to use this, please set const excludeSheetnames = [];
. By this, all sheets are used.
// First, please run this function. By this, all sheets are scanned and set the tab colors.
function forInitialSetting() {
const excludeSheetnames = ["Sheet1"]; // If you want to exclude the sheets, please set the sheet names you want to exclude.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const sheets = ss.getSheets();
const redColor = { red: 1, green: 0, blue: 0 }; // <-- #ff0000 (red)
const greenColor = { red: 0, green: 1, blue: 0 }; // <-- #00ff00 (green)
const { ranges, requests } = sheets.reduce((o, s) => {
const sheetName = s.getSheetName();
if (!excludeSheetnames.includes(sheetName)) {
o.ranges.push(`'${sheetName}'!A1`);
o.requests.push({ updateSheetProperties: { properties: { sheetId: s.getSheetId() }, fields: "tabColor" } });
}
return o;
}, { ranges: [], requests: [] });
const v = Sheets.Spreadsheets.Values.batchGet(ssId, { ranges }).valueRanges;
v.forEach(({ values }, i) =>
requests[i].updateSheetProperties.properties.tabColor = values && values[0][0] ? greenColor : redColor
);
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
// This script is automatically run by the simple trigger when the cell "A1" is edited.
// Please don't directly run with the script editor.
function onEdit(e) {
const excludeSheetnames = ["Sheet1"]; // If you want to exclude the sheets, please set the sheet names you want to exclude.
const range = e.range;
const sheet = range.getSheet();
if (e.range.getA1Notation() != "A1" || excludeSheetnames.includes(sheet.getSheetName())) return;
sheet.setTabColor(range.getDisplayValue() ? "#00ff00" : "#ff0000");
}
forInitialSetting
with the script editor. By this, all sheets are scanned and set the tab colors.forInitialSetting
, when you edit the cell "A1", onEdit
function is automatically run by the OnEdit trigger. By this, when the cell "A1" has a value, the tab color is set to "green". When the cell "A1" has no value, the tab color is set to "red". For example, if you want to run forInitialSetting
every editing the cell "A1", in this case, please add the following function and install OnEdit trigger to the function installedOnEdit
. In that case, please remove function onEdit(e) {,,,}
. By this, when the cell "A1" is edited, installedOnEdit
is run.
function installedOnEdit(e) { const excludeSheetnames = ["Sheet1"]; // If you want to exclude the sheets, please set the sheet names you want to exclude. const range = e.range; const sheet = range.getSheet(); if (e.range.getA1Notation() != "A1" || excludeSheetnames.includes(sheet.getSheetName())) return; forInitialSetting(); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.