maria
maria

Reputation: 159

Google Apps Script: Apply formatting rule to a spesific sheet

according to documents: https://developers.google.com/apps-script/reference/spreadsheet/conditional-format-rule-builder

var sheet = SpreadsheetApp.getActiveSheet();

But i only want it to be in a spesific sheet called Resultat.

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

But with this sheetByName, i get :

TypeError: ThisSheet.newConditionalFormatRule is not a function

code to call:

 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);
  

Upvotes: 1

Views: 1204

Answers (1)

Tanaike
Tanaike

Reputation: 201388

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:

Upvotes: 2

Related Questions