Search code examples
google-apps-scriptgoogle-sheetscustom-function

Custom Condition Formatting, how to work with a String amount based condition in a Cell


I want to format cells with a custom function. The cells have the content like so (without the ") :

Cell_1: "String1 String2 String3"
Cell_2: ""
Cell_3: "String1 String2 String3 String4 String5 String6"

if there is at least like 5 Strings in it, separated with a space it should be a true statement.

I tried to use a custom script like so, because im calling a function in this cell anyway, before returning the value i could set the format too:

var self = SpreadsheetApp.getActiveSheet().getActiveCell();
self.setFontWeight("normal");
if(returnValue.length>=5){
  self.setFontWeight("bold");
}
return returnValue.sort().join(" ");

But i got a "You do not have permission to call setFontWeight" Exeption. After a little research i found that cunstom scripts are not allowed to cange format at all. Fine ... but how can i do it with the custom formatting condition

is something like this possible?:

=If(Split(?inputRange?;" ").length>=5;True;False)

If Yes how do i get the Range in the right format and which function is needed to get the String amount?

i tried also to write a custom function for that purpose:

function customFormat(input){
if(input.length>=5)
   return true;
}
return false;

calling like so as custom condition:

=customFormat(range)

it seams like i cant use custom function there, i get no positiv result even than i return true always.

If you got some ideas how to accomplish this, i would be thankfull.


Solution

  • Assuming those three cells are in A1:A3

    Apply conditional formatting to that range with the formula

    =COUNTA(SPLIT(A1, " ")) >= 5