Conditional Formatting with Scripts and Power Automate

%3CLINGO-SUB%20id%3D%22lingo-sub-2311795%22%20slang%3D%22en-US%22%3EConditional%20Formatting%20with%20Scripts%20and%20Power%20Automate%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2311795%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20an%20Excel%20Spreadsheet%20that%20two%20users%20use%20each%20day.%3C%2FP%3E%0A%3CP%3EThey%20do%20a%20LOT%20of%20ctrl%20C%20and%20ctrl%20V.%3C%2FP%3E%0A%3CP%3EBecause%20of%20this%2C%20it%20starts%20messing%20with%20the%20conditional%20formats%20we%20have.%3C%2FP%3E%0A%3CP%3EMy%20idea%20was%20to%20create%20a%20script%20inside%20of%20Excel%20Online%2C%20save%20it%2C%20then%20user%20Power%20Automate%20each%20night%20to%20run%20that%20script%20to%20fix%20all%20the%20conditional%20formats.%3C%2FP%3E%0A%3CP%3EThe%20script%20would%20delete%20the%20CFs%20from%20two%20sections%20of%20the%20sheet%2C%20and%20then%20recreate%20them.%3C%2FP%3E%0A%3CP%3EI%20have%20zero%20idea%20how%20to%20write%20these%20scripts%2C%20so%20I%20was%20using%20the%20record%20actions%20feature.%3C%2FP%3E%0A%3CP%3EIt%20records%20me%20creating%20the%20CF%2C%20but%20when%20I%20run%20the%20script%20to%20test%20it%2C%20it%20always%20errors.%3C%2FP%3E%0A%3CP%3ESee%20attached.%3C%2FP%3E%0A%3CP%3EWhat%20I%20am%20not%20understanding%20is%20I%20am%20not%20changing%20these%20scripts%20after%20I%20stop%20recording%20so%20I%20am%20not%20sure%20what%20is%20happening.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGoal%3A%3C%2FP%3E%0A%3CP%3EScript%20the%20following%3A%3C%2FP%3E%0A%3CP%3EDelete%20all%20rules%20in%20section%20J3%3AL55%3C%2FP%3E%0A%3CP%3EDelete%20all%20rules%20in%20section%20M3%3AAG66%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20section%20J3%3AL55%2C%20create%20a%20rule%20that%20highlights%20cells%20that%20have%20text%20like%3A%3C%2FP%3E%0A%3CP%3ESun-Wed%3C%2FP%3E%0A%3CP%3EOO*%3C%2FP%3E%0A%3CP%3EOFF*%3C%2FP%3E%0A%3CP%3EWed-Sat%3C%2FP%3E%0A%3CP%3EMed*%3C%2FP%3E%0A%3CP%3Eetc..%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20the%20other%20sections%2C%20I%20have%20a%20rule%20for%20each%20column%20that%20turns%20the%20cells%20black%20if%20a%20certain%20cell%20is%20a%20Weekend.%26nbsp%3B%20The%20actual%20condition%20looks%20like%20this%3A%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3EApply%20to%20range%20M3%3AM55%3C%2FP%3E%0A%3CP%3E%3DWEEKDAY(R2C13%2C2)%26gt%3B5%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20I%20do%20that%20for%20every%20other%20column%20(the%20C%20changes%20in%20the%20formula%20above).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20I%20try%20to%20create%20these%20with%20the%20recorder%20they%20are%20failing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2311795%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20Scripts%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2311926%22%20slang%3D%22en-US%22%3ERE%3A%20Conditional%20Formatting%20with%20Scripts%20and%20Power%20Automate%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2311926%22%20slang%3D%22en-US%22%3EOk%2C%20looks%20like%20the%20record%20action%20for%20the%20second%20section%20(marking%20it%20as%20weekend)%20is%20working.%20i%20was%20able%20to%20record%20that%2C%20delete%20the%20condition%2C%20and%20run%20the%20script%2C%20that%20worked.%20I%20still%20need%20assistance%20with%20the%20text%20conditions%20in%20the%20first%20section%3C%2FLINGO-BODY%3E
Microsoft

I have an Excel Spreadsheet that two users use each day.

They do a LOT of ctrl C and ctrl V.

Because of this, it starts messing with the conditional formats we have.

My idea was to create a script inside of Excel Online, save it, then user Power Automate each night to run that script to fix all the conditional formats.

The script would delete the CFs from two sections of the sheet, and then recreate them.

I have zero idea how to write these scripts, so I was using the record actions feature.

It records me creating the CF, but when I run the script to test it, it always errors.

See attached.

What I am not understanding is I am not changing these scripts after I stop recording so I am not sure what is happening.

 

Goal:

Script the following:

Delete all rules in section J3:L55

Delete all rules in section M3:AG66

 

For section J3:L55, create a rule that highlights cells that have text like:

Sun-Wed

OO*

OFF*

Wed-Sat

Med*

etc..

 

For the other sections, I have a rule for each column that turns the cells black if a certain cell is a Weekend.  The actual condition looks like this:  

Apply to range M3:M55

=WEEKDAY(R2C13,2)>5

 

Then I do that for every other column (the C changes in the formula above).

 

When I try to create these with the recorder they are failing.

 

3 Replies
Ok, looks like the record action for the second section (marking it as weekend) is working. i was able to record that, delete the condition, and run the script, that worked. I still need assistance with the text conditions in the first section
Is there a limit with scripts? I found if I run the following my conditions work up to a certain column, then they dont work. If I clear the rules for the entire sheet, and enter the formatting into a column that was not working, it will work. let selectedSheet = workbook.getActiveWorksheet(); // Clear conditional formats from range M:AG on selectedSheet selectedSheet.getRange("M:AG").clearAllConditionalFormats(); let conditionalFormatting: ExcelScript.ConditionalFormat; // Change custom from range M3:M55 on selectedSheet conditionalFormatting = selectedSheet.getRange("M3:M55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+1,2)>5"); // Change custom from range N3:N55 on selectedSheet conditionalFormatting = selectedSheet.getRange("N3:N55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+2,2)>5"); // Change custom from range O3:O55 on selectedSheet conditionalFormatting = selectedSheet.getRange("O3:O55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+3,2)>5"); // Change custom from range P3:P55 on selectedSheet conditionalFormatting = selectedSheet.getRange("P3:P55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+4,2)>5"); // Change custom from range Q3:Q55 on selectedSheet conditionalFormatting = selectedSheet.getRange("Q3:Q55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+5,2)>5"); // Change custom from range R3:R55 on selectedSheet conditionalFormatting = selectedSheet.getRange("R3:R55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+6,2)>5"); // Change custom from range S3:S55 on selectedSheet conditionalFormatting = selectedSheet.getRange("S3:S55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+7,2)>5"); // Change custom from range T3:T55 on selectedSheet conditionalFormatting = selectedSheet.getRange("T3:T55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+8,2)>5"); // Change custom from range U3:U55 on selectedSheet conditionalFormatting = selectedSheet.getRange("U3:U55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+9,2)>5"); // Change custom from range V3:V55 on selectedSheet conditionalFormatting = selectedSheet.getRange("V3:V55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+10,2)>5"); // Change custom from range W3:W55 on selectedSheet conditionalFormatting = selectedSheet.getRange("W3:W55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+11,2)>5"); // Change custom from range X3:X55 on selectedSheet conditionalFormatting = selectedSheet.getRange("X3:X55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKDAY(Today()+12,2)>5"); // Change custom from range Y3:Y55 on selectedSheet conditionalFormatting = selectedSheet.getRange("Y3:Y55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+13,2)>5"); // Change custom from range Z3:Z55 on selectedSheet conditionalFormatting = selectedSheet.getRange("Z3:Z55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+14,2)>5"); // Change custom from range AA3:AA55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AA3:AA55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+15,2)>5"); // Change custom from range AB3:AB55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AB3:AB55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+16,2)>5"); // Change custom from range AC3:AC55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AC3:AC55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+17,2)>5"); // Change custom from range AD3:AD55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AD3:AD55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+18,2)>5"); // Change custom from range AE3:AE55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AE3:AE55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+19,2)>5"); // Change custom from range AF3:AF55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AF3:AF55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+20,2)>5"); // Change custom from range AG3:AG55 on selectedSheet conditionalFormatting = selectedSheet.getRange("AG3:AG55").addConditionalFormat(ExcelScript.ConditionalFormatType.custom); conditionalFormatting.getCustom().getFormat().getFont().setColor("#9C0006"); conditionalFormatting.getCustom().getFormat().getFill().setColor("#000000"); conditionalFormatting.getCustom().getRule().setFormula("=WEEKEND(Today()+21,2)>5");
I fixed all the issues except for the conditions of text, if anyone can help me out