-
-
Notifications
You must be signed in to change notification settings - Fork 36
newConditionalFormatting
Julian Halliwell edited this page May 9, 2023
·
2 revisions
Returns a new conditionalFormatting object to be applied to a workbook.
newConditionalFormatting()
- Conditional formatting allows cells to be highlighted or otherwise visually formatted when they contain particular values or when a given condition is met.
- ConditionalFormatting objects are created using a "builder" syntax (see examples below).
- You can apply the ConditionalFormatting object either by passing in the workbook to the
addToWorkbook()
builder method, or by returning the new ConditionalFormatting object and passing it to the library's addConditionalFormatting() method.
-
when( required string formula )
: use to specify any condition or formula that if true will cause the formatting to be applied. -
whenCellValueIs( required string comparisonOperator, required string valueOrFormula, string valueOrFormula2 )
: use to apply the formatting when the cell's value meets a condition specified using a comparison operator and a value or formula to compare the cell's value with (see examples below).- Possible
comparisonOperator
values are:EQ
,NEQ
,GT
,LT
,GTE
,LTE
,BETWEEN
andNOT BETWEEN
. - with
BETWEEN
andNOT BETWEEN
, you must specify thevalueOrFormula2
for the comparison.
- Possible
-
setFormat( required struct format )
: pass in a struct which specifies the format to be applied when the condition is met (see options below). -
onCells( required string cellRangeReference )
: specify which cells to apply the conditional formatting to as a cell range reference, e.g. "A1" or "A1:B4". -
onSheetName( required string sheetName )
: By default the currently active sheet will be used, but you can specify a different sheet by passing the name. -
onSheetNumber( required numeric sheetNumber)
: By default the currently active sheet will be used, but you can specify a different sheet by passing the number. -
addToWorkbook( required workbook )
: applies the conditionalFormatting object to the specified workbook.
Note: the options available for conditional formatting are more limited than those possible with the standard formatting methods.
-
fontColor
(string): Can be any of the following:- The name of a pre-set colour - full list of available colours, also viewable via the
getPresetColorNames()
method - an RGB triplet, e.g.
255,255,255
. NB: Only XLSX files can use exact RGB specified colours. Binary XLS files will use the closest pre-defined colour. - a hex value, e.g. "4B0082" or "#4B0082", which will be converted to an RGB triplet
- The name of a pre-set colour - full list of available colours, also viewable via the
-
fontSize
(integer): size in points. -
bold
(boolean) -
italic
(boolean) -
underline
(boolean OR string): Either true/false or one of the following: "none", "single", or "double". -
bottomBorder
(string): One of the POI Border Styles -
bottomBorderColor
(string): SeefontColor
for possible values -
leftBorder
(string): SeebottomBorder
for possible values -
leftBorderColor
(string): SeefontColor
for possible values -
rightBorder
(string): SeebottomBorder
for possible values -
rightBorderColor
(string): SeefontColor
for possible values -
topBorder
(string): SeebottomBorder
for possible values -
topBorderColor
(string): SeefontColor
for possible values -
backgroundFillColor
(string): SeefontColor
for possible values, although the options may be more limited. -
foregroundFillColor
(string): SeefontColor
for possible values, although the options may be more limited. -
fillPattern
(string): One of the POI fill patterns
wb = spreadsheet.newXlsx();
spreadsheet.newConditionalFormatting()
.onCells( "A1:A10" )
.whenCellValueIs( "LT", 0 )
.setFormat( { backgroundFillColor: "RED", bold: true } )
.addToWorkbook( wb );
// set A1 to 1, the format is unchanged
spreadsheet.setCellValue( wb, 1, 1, 1 );
// set A2 to -1, it goes red/bold
spreadsheet.setCellValue( wb, -1, 2, 1 );
wb = spreadsheet.newXlsx();
spreadsheet.newConditionalFormatting()
.onCells( "A1:B1" )
.when( "$A1<0" )
.setFormat( { backgroundFillColor: "RED", bold: true } )
.addToWorkbook( wb );
// set B1 as a label
spreadsheet.setCellValue( wb, "Warning label", 1, 2 );
// set A1 to 1, the format is unchanged
spreadsheet.setCellValue( wb, 1, 1, 1 );
// set A1 to -1, both A1 and B1 go red/bold
spreadsheet.setCellValue( wb, -1, 1, 1 );
formatting = spreadsheet.newConditionalFormatting()
.onCells( "A1:B1" )
.when( "$A1<0" )
.setFormat( { backgroundFillColor: "RED", bold: true } );
chainable = spreadsheet.newChainable( "xlsx" ).addConditionalFormatting( formatting );
);