1

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); 
3
  • 1
    Is that a typo? you wanted sheet instead of ThisSheet ? where is ThisSheet defined? Commented Feb 22, 2021 at 12:29
  • ur right @marios Commented Feb 22, 2021 at 13:32
  • @maria jsfiddle.net/q8fcsL05 (unrelated) Commented Mar 30, 2021 at 20:05

1 Answer 1

2

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:

Sign up to request clarification or add additional context in comments.

2 Comments

thissheet is reallt the sheet var, as you mentioned.
@maria Thank you for replying. I'm glad your issue was resolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.