Google Apps 腳本:將格式規則應用於特定工作表 (Google Apps Script: Apply formatting rule to a spesific sheet)


問題描述

Google Apps 腳本:將格式規則應用於特定工作表 (Google Apps Script: Apply formatting rule to a spesific sheet)

根據文檔:https://developers .google.com/apps‑script/reference/spreadsheet/conditional‑format‑rule‑builder

var sheet = SpreadsheetApp.getActiveSheet();

但我只希望它出現在名為 Resultat 的特定工作表中。

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");

但是有了這個 sheetByName,我得到:

TypeError: ThisSheet.newConditionalFormatRule is not a function

調用代碼:

 var ThisSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");
var range = ThisSheet.getRange("C:D") // only want it to select C element.
ThisSheet.clearConditionalFormatRules();
var rule1 = ThisSheet.newConditionalFormatRule()
.whenTextContains("NO")
.setBackground("#b3cfb0")
.setRanges([range])
.build();
var rules = ThisSheet.getConditionalFormatRules();
rules.push(rule1);
ThisSheet.setConditionalFormatRules(rules);

</code></pre>


參考解法

方法 1:

Modification points:

  • From your script, I cannot understand about ThisSheet. If ThisSheet is sheet of var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");, newConditionalFormatRule() is the method of Class SpreadsheetApp. By this, the error occurs.

When ThisSheet is sheet of var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");, how about the following modification?

Modified script:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");
var range = sheet.getRange("C:D") // only want it to select C element.
sheet.clearConditionalFormatRules();
var rule1 = SpreadsheetApp.newConditionalFormatRule()
  .whenTextContains("NO")
  .setBackground("#b3cfb0")
  .setRanges([range])
  .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule1);
sheet.setConditionalFormatRules(rules);
  • By above modified script, the conditional format rule is set to the range of "C:D".
  • If you want to set the conditional format rule to the column "C", please modify var range = sheet.getRange("C:D") to var range = sheet.getRange("C:C").

Reference:

(by mariaTanaike)

參考文件

  1. Google Apps Script: Apply formatting rule to a spesific sheet (CC BY‑SA 2.5/3.0/4.0)

#google-sheets #google-apps-script #conditional-formatting






相關問題

通過 JavaScript 客戶端 API Auth 使用 Google 電子表格作為“已驗證”數據源的 Google 可視化 API (Google Visualization API using Google Spreadsheet as "Authenticated" Data Source via JavaScript Client API Auth)

根據另一個單元格的值更改單元格的背景 (Changing background of a cell based on the value of another)

如何從 Google 表格中的字符串中刪除一組數字 (how to remove a set of digits from string in Google Sheets)

如何將 3 個不同的工作表與 Query(importrange) 結合起來? (How to combine 3 different sheets with Query(importrange)?)

如何在 Google 表格中使用 ArrayFormula 計算總數和唯一性(基於 2 個單獨的列)? (How to count totals and uniques (based on 2 separate columns) using ArrayFormula in Google Sheets?)

如何根據查看文檔的 Google 帳戶對 Google 表格中的列進行編程,使其從用戶輸入中灰顯 (How to program a column in Google sheets to be grayed out from user input depending on the Google account viewing the document)

單擊單元格時的谷歌應用程序腳本分配功能 (google application script assign function when cell is clicked)

如何將日期後的第一個值從一個選項卡拉到谷歌表格中的另一個選項卡中? (How to pull the first value after a date from one tab into another in google sheets?)

一次在多行上的 OnEdit 時間戳 (OnEdit timestamp on several rows at one time)

Google Sheets Script,如何創建工作表標籤的新鬆散對象 (Google Sheets Script, how to create a NEW loose object of a sheet tab)

Google Apps 腳本:將格式規則應用於特定工作表 (Google Apps Script: Apply formatting rule to a spesific sheet)

找到更接近的值並複制它 (Finding a closer value and copying it)







留言討論