Conditional formatting is applied using the format condition object in the Excel Suite. You will note that it is an element of range class, which may have none, or several of them. (It seems to be limited here to three per range.) Each conditional format may have at most three conditions, and the formats that can be set are for font, interior (background), and border. For example, in Visual Basic for Applications (VBA), the following applies a bold green font to values between 0 and 10:
With ActiveSheet.Range("B1").FormatConditions
.Delete 'Delete existing conditional formats
With .Add(Type:=xlCellValue, _
Operator:=xlBetween, _
Formula1:="0", _
Formula2:="10").Font
.Bold = True
.Italic = False
.ColorIndex = 4
End With
End WithHere is the AppleScript version:
tell application "Microsoft Excel"
tell range "B1" of active sheet
try
delete (every format condition)
end try
set newFormatCondition to make new format condition ¬
at end with properties {format condition type: ¬
cell value, condition operator:operator between, ¬
formula1:"0", formula2:"10"}
tell font object of newFormatCondition
set {bold, italic, font color index} ¬
to {true, false, 4}
end tell
end tell
end tellFormat conditions are elements, not properties, of the range object, so you can delete any existing ones before making new ones. This is a good idea when you are making your first format condition for a range in a workbook that may have passed through other hands or has been around. (If you go on to make another format condition at the same range for different conditions, omit the delete command if you want to keep the ones you've just made.) Put the delete command in a
try
block because it will error if there are no existing format conditions.
You will see from the dictionary that every property of the format condition element is listed as read-only, so how can you set anything? There are many classes, at least in other applications, with properties listed in the dictionary as read-only but which can be set at inception when using
make new with properties {...}
only, and never afterwards. It is quite common to find these in other applications.
There do not seem to be many of these kinds of classes in Microsoft Excel, Word, nor PowerPoint. However, many "more standard" scriptable applications, such as Microsoft Entourage, are full of such classes. It is always worth checking and testing for these.
The properties here that can be set at inception are the enumerations and text properties: format condition type, condition operator, formula1, and formula2, that is, the conditions. But if you try to include settings for the font object or interior object within the properties for
make new format condition, they will be ignored as always. Instead, although you can't set a font object itself, you can set its own properties (bold, italic, name, font color index, etc.) after the format condition element in which the font object resides is made. This is quite normal for the Microsoft Office
applications. It is true for all of the complex objects, such as fonts, that other objects can have as their properties.
Also, the "simple" read-only properties that are not objects and don't have properties of their own that can be set afterwards, can be set at inception. This makes these format condition objects very useful and scriptable.
One other thing to note is that these are elements that must be made
at beginning
or
at end
of the range — it doesn't matter which (except perhaps to primacy if you overlap two conflicting conditions).
You may also have noticed that the dictionary lists
formula 1
and
formula 2
as the parameter names but they compile to formula1 and formula2 without spaces, just as they appear in add data validation. That means they've been implemented as synonyms: you can type either and both versions compile.
You can apply more complex formats based on formula conditions by declaring the Type to be expression (xlExpression in VBA), meaning a formula.
With ActiveSheet.Range("B1").FormatConditions
.Delete
With .Add(Type:=xlExpression, _
Formula1:="=AND($A1>0,$A1<10)")
With .Font
.Bold = True
.Italic = False
.ColorIndex = 2
End With
.Interior.ColorIndex = 3
End With
End Withtell application "Microsoft Excel"
tell range "B1" of active sheet
try
delete (every format condition)
end try
set newFormatCondition to make new format condition ¬
at end with properties {format condition type:¬
expression, formula1: "=AND($A1>0,$A1<10)"}
tell newFormatCondition
tell its font object
set {bold, italic, font color index} ¬
to {true, false, 2}
end tell
set color index of its interior object to 3
end tell
end tell
end tellThis turns the background red and font bold white in
B2, when there's a number from 1 to 9 in
A1. It works perfectly in AppleScript.
Format conditions can also format borders, fonts, and interiors. In VBA, the Borders Collection is a property of the FormatCondition object and works the same way as fonts and interiors do. In AppleScript, you have to use the get border command targeted at the format condition element, as found in the Microsoft Excel Suite. It's listed as a different get border command in a different suite, so as to be able to specify the type of object it works on — format condition rather than range. But actually it's the same old format condition command and works identically to the one you used with range. It returns a border of the type you specify with the which border parameter, and you then apply the formatting to each border you retrieve.


