![](/img/trans.png)
[英]Google Apps Script - inserting a string in HTML based on if/else statement and based on whether the sheet cell is blank
[英]Change sheet tab color based on blank/no blank cell
我有一個超過 50 張的 Google 工作表。 每個工作表選項卡都標有負責在該工作表中進行更改的人員的姓名。
我想要一個腳本,使所有工作表選項卡變為紅色,直到 A2 不再為空白。 當 A2 包含文本時,工作表選項卡將變為綠色。 如果 A2 中的文本被擦除使單元格再次變為空白,則工作表選項卡應變回紅色。
我是腳本新手,嘗試了一些在互聯網上找到的片段,但它們都在 A2 中尋找特定文本來更改標簽顏色。 我需要根據單元格為空白或非空白來更改選項卡顏色。
謝謝您的幫助!
function greenAndRed() {
const ss = SpreadsheetApp.getActive();
ss.getSheets().forEach(sh => {
if(sh.getRange("A2").isBlank()) {
sh.setTabColor("#ff0000");//red
} else {
sh.setTabColor("#00ff00");//green
}
});
}
我相信你的目標如下。
我認為在您的情況下,每次運行都掃描所有工作表時,流程成本會變高。 那么,在這個答案中,以下流程如何?
這里使用了 2 個函數。 示例腳本如下。
請將以下腳本復制並粘貼到 Google 電子表格的腳本編輯器中。 並且,在這個示例中,為了降低腳本的處理成本,使用了 Sheets API。 因此, 請在 Advanced Google services 中啟用 Sheets API 。
在此示例腳本中,您可以設置排除的工作表。 如果要排除用於設置標簽顏色的工作表,請將要排除的工作表名稱設置為excludeSheetnames
變量。 如果您不想使用它,請設置const excludeSheetnames = [];
. 這樣,所有的片材都被使用了。
// 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
。 這樣,所有工作表都被掃描並設置選項卡顏色。forInitialSetting
后,當您編輯單元格“A1”時,OnEdit 觸發器會自動運行onEdit
函數。 這樣,當單元格“A1”具有值時,選項卡顏色設置為“綠色”。 當單元格“A1”沒有值時,選項卡顏色設置為“紅色”。 例如,如果您想在每次編輯單元格“A1”時運行forInitialSetting
,在這種情況下,請添加以下函數並將 OnEdit 觸發器安裝到函數installedOnEdit
中。 在這種情況下,請刪除function onEdit(e) {,,,}
。 這樣,當單元格“A1”被編輯時, installedOnEdit
運行。
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(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.