\r
import org.apache.poi.hssf.usermodel.*;\r
import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;\r
+import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;\r
import org.apache.poi.ss.util.CellRangeAddress;\r
import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
\r
* Excel Conditional Formatting -- Examples\r
*\r
* <p>\r
- * Based on the code snippets from http://www.contextures.com/xlcondformat03.html\r
+ * Partly based on the code snippets from \r
+ * http://www.contextures.com/xlcondformat03.html\r
* </p>\r
- *\r
- * @author Yegor Kozlov\r
*/\r
public class ConditionalFormats {\r
\r
expiry(wb.createSheet("Expiry"));\r
shadeAlt(wb.createSheet("Shade Alt"));\r
shadeBands(wb.createSheet("Shade Bands"));\r
+ iconSets(wb.createSheet("Icon Sets"));\r
\r
- // TODO Add Icons, data bars etc, see bug #58130\r
+ // TODO Add colour scales, data bars etc, see bug #58130\r
\r
// Write the output to a file\r
String file = "cf-poi.xls";\r
sheet.createRow(0).createCell(1).setCellValue("Shade Bands of Rows");\r
sheet.createRow(1).createCell(1).setCellValue("Condition: Formula Is =MOD(ROW(),6)<2 (Light Grey Fill)");\r
}\r
+ \r
+ /**\r
+ * Icon Sets / Multi-States allow you to have icons shown which vary\r
+ * based on the values, eg Red traffic light / Yellow traffic light /\r
+ * Green traffic light\r
+ */\r
+ static void iconSets(Sheet sheet) {\r
+ sheet.createRow(0).createCell(0).setCellValue("Icon Sets");\r
+ Row r = sheet.createRow(1);\r
+ r.createCell(0).setCellValue("Reds");\r
+ r.createCell(1).setCellValue(0);\r
+ r.createCell(2).setCellValue(0);\r
+ r.createCell(3).setCellValue(0);\r
+ r = sheet.createRow(2);\r
+ r.createCell(0).setCellValue("Yellows");\r
+ r.createCell(1).setCellValue(5);\r
+ r.createCell(2).setCellValue(5);\r
+ r.createCell(3).setCellValue(5);\r
+ r = sheet.createRow(3);\r
+ r.createCell(0).setCellValue("Greens");\r
+ r.createCell(1).setCellValue(10);\r
+ r.createCell(2).setCellValue(10);\r
+ r.createCell(3).setCellValue(10);\r
+ \r
+ SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();\r
+ \r
+ CellRangeAddress[] regions = { CellRangeAddress.valueOf("B1:B4") };\r
+ ConditionalFormattingRule rule1 =\r
+ sheetCF.createConditionalFormattingRule(IconSet.GYR_3_TRAFFIC_LIGHTS);\r
+ IconMultiStateFormatting im1 = rule1.getMultiStateFormatting();\r
+ im1.getThresholds()[0].setRangeType(RangeType.MIN);\r
+ im1.getThresholds()[1].setRangeType(RangeType.PERCENT);\r
+ im1.getThresholds()[1].setValue(33d);\r
+ im1.getThresholds()[2].setRangeType(RangeType.MAX);\r
+ sheetCF.addConditionalFormatting(regions, rule1);\r
+ \r
+ regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C1:C4") };\r
+ ConditionalFormattingRule rule2 =\r
+ sheetCF.createConditionalFormattingRule(IconSet.GYR_3_FLAGS);\r
+ IconMultiStateFormatting im2 = rule1.getMultiStateFormatting();\r
+ im2.getThresholds()[0].setRangeType(RangeType.PERCENT);\r
+ im2.getThresholds()[0].setValue(0d);\r
+ im2.getThresholds()[1].setRangeType(RangeType.PERCENT);\r
+ im2.getThresholds()[1].setValue(33d);\r
+ im2.getThresholds()[2].setRangeType(RangeType.PERCENT);\r
+ im2.getThresholds()[2].setValue(67d);\r
+ sheetCF.addConditionalFormatting(regions, rule2);\r
+ \r
+ regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D1:D4") };\r
+ ConditionalFormattingRule rule3 =\r
+ sheetCF.createConditionalFormattingRule(IconSet.GYR_3_SYMBOLS_CIRCLE);\r
+ IconMultiStateFormatting im3 = rule1.getMultiStateFormatting();\r
+ im3.setIconOnly(true);\r
+ im3.getThresholds()[0].setRangeType(RangeType.MIN);\r
+ im3.getThresholds()[1].setRangeType(RangeType.NUMBER);\r
+ im3.getThresholds()[1].setValue(3d);\r
+ im3.getThresholds()[2].setRangeType(RangeType.NUMBER);\r
+ im3.getThresholds()[2].setValue(7d);\r
+ sheetCF.addConditionalFormatting(regions, rule3);\r
+ }\r
}\r
import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;\r
import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;\r
import org.apache.poi.ss.ITestDataProvider;\r
+import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;\r
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;\r
import org.apache.poi.ss.util.CellRangeAddress;\r
\r
ConditionalFormatting cf = null;\r
ConditionalFormattingRule cr = null;\r
IconMultiStateFormatting icon = null;\r
+ ConditionalFormattingThreshold th = null;\r
\r
// Sanity check data\r
assertEquals("Values", s.getRow(0).getCell(0).toString());\r
type == ConditionType.FORMULA) {\r
fCF++;\r
} else {\r
- // TODO Detect Ext ones\r
+ // TODO Properly detect Ext ones from the xml\r
fCF12++;\r
}\r
}\r
// When it matches:\r
// Sets the font colour to dark green\r
// Sets the background colour to lighter green\r
- // TODO Should the colours be slightly different between formats?\r
+ // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?\r
if (cr instanceof HSSFConditionalFormattingRule) {\r
assertColour("0:8080:0", cr.getFontFormatting().getFontColor());\r
assertColour("CCCC:FFFF:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());\r
// When it matches:\r
// Sets the font colour to dark red\r
// Sets the background colour to lighter red\r
- // TODO Should the colours be slightly different between formats?\r
+ // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?\r
if (cr instanceof HSSFConditionalFormattingRule) {\r
assertColour("8080:0:8080", cr.getFontFormatting().getFontColor());\r
assertColour("FFFF:9999:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());\r
assertEquals(1, cf.getNumberOfRules());\r
cr = cf.getRule(0);\r
assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType());\r
- // TODO Support then check the rest\r
+ // TODO Support Data Bars, then check the rest of this rule\r
\r
\r
// Colours R->G - Column F\r
+ cf = sheetCF.getConditionalFormattingAt(3);\r
+ assertEquals(1, cf.getFormattingRanges().length);\r
+ assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString());\r
+ \r
+ assertEquals(1, cf.getNumberOfRules());\r
+ cr = cf.getRule(0);\r
+ assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType());\r
+ // TODO Support Color Scales, then check the rest of this rule\r
+\r
+ \r
// Colours BWR - Column G\r
+ cf = sheetCF.getConditionalFormattingAt(4);\r
+ assertEquals(1, cf.getFormattingRanges().length);\r
+ assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString());\r
+ \r
+ assertEquals(1, cf.getNumberOfRules());\r
+ cr = cf.getRule(0);\r
+ assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType());\r
+ // TODO Support Color Scales, then check the rest of this rule\r
+\r
+ \r
+ // TODO Simplify asserts\r
\r
- // Icons : Default - Column H\r
+ // Icons : Default - Column H, percentage thresholds\r
cf = sheetCF.getConditionalFormattingAt(5);\r
assertEquals(1, cf.getFormattingRanges().length);\r
assertEquals("H2:H17", cf.getFormattingRanges()[0].formatAsString());\r
assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());\r
assertEquals(null, cr.getFormula1());\r
assertEquals(null, cr.getFormula2());\r
- if (cr instanceof HSSFConditionalFormattingRule) {\r
- HSSFConditionalFormattingRule hcr = (HSSFConditionalFormattingRule)cr;\r
- icon = hcr.getMultiStateFormatting();\r
- assertNotNull(icon);\r
- assertEquals(IconSet.GYR_3_TRAFFIC_LIGHTS, icon.getIconSet());\r
- assertEquals(false, icon.isIconOnly());\r
- assertEquals(false, icon.isReversed());\r
- // TODO Check the rest\r
- } else {\r
- // TODO XSSF Support\r
- }\r
+ \r
+ icon = cr.getMultiStateFormatting();\r
+ assertNotNull(icon);\r
+ assertEquals(IconSet.GYR_3_TRAFFIC_LIGHTS, icon.getIconSet());\r
+ assertEquals(false, icon.isIconOnly());\r
+ assertEquals(false, icon.isReversed());\r
+ \r
+ assertNotNull(icon.getThresholds());\r
+ assertEquals(3, icon.getThresholds().length);\r
+ th = icon.getThresholds()[0];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(0.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[1];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(33.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[2];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(67.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ \r
\r
// Icons : 3 signs - Column I\r
+ cf = sheetCF.getConditionalFormattingAt(6);\r
+ assertEquals(1, cf.getFormattingRanges().length);\r
+ assertEquals("I2:I17", cf.getFormattingRanges()[0].formatAsString());\r
+ \r
+ assertEquals(1, cf.getNumberOfRules());\r
+ cr = cf.getRule(0);\r
+ assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType());\r
+ assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());\r
+ assertEquals(null, cr.getFormula1());\r
+ assertEquals(null, cr.getFormula2());\r
+ \r
+ icon = cr.getMultiStateFormatting();\r
+ assertNotNull(icon);\r
+ assertEquals(IconSet.GYR_3_SHAPES, icon.getIconSet());\r
+ assertEquals(false, icon.isIconOnly());\r
+ assertEquals(false, icon.isReversed());\r
+ \r
+ assertNotNull(icon.getThresholds());\r
+ assertEquals(3, icon.getThresholds().length);\r
+ th = icon.getThresholds()[0];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(0.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[1];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(33.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[2];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(67.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ \r
+ \r
// Icons : 3 traffic lights 2 - Column J\r
+ cf = sheetCF.getConditionalFormattingAt(7);\r
+ assertEquals(1, cf.getFormattingRanges().length);\r
+ assertEquals("J2:J17", cf.getFormattingRanges()[0].formatAsString());\r
+ \r
+ assertEquals(1, cf.getNumberOfRules());\r
+ cr = cf.getRule(0);\r
+ assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType());\r
+ assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());\r
+ assertEquals(null, cr.getFormula1());\r
+ assertEquals(null, cr.getFormula2());\r
+ \r
+ icon = cr.getMultiStateFormatting();\r
+ assertNotNull(icon);\r
+ assertEquals(IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, icon.getIconSet());\r
+ assertEquals(false, icon.isIconOnly());\r
+ assertEquals(false, icon.isReversed());\r
+ \r
+ assertNotNull(icon.getThresholds());\r
+ assertEquals(3, icon.getThresholds().length);\r
+ th = icon.getThresholds()[0];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(0.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[1];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(33.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ th = icon.getThresholds()[2];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(67.0d, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ \r
+ \r
// Icons : 4 traffic lights - Column K\r
+ cf = sheetCF.getConditionalFormattingAt(8);\r
+ assertEquals(1, cf.getFormattingRanges().length);\r
+ assertEquals("K2:K17", cf.getFormattingRanges()[0].formatAsString());\r
+ \r
+ assertEquals(1, cf.getNumberOfRules());\r
+ cr = cf.getRule(0);\r
+ assertIconSetPercentages(cr, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d);\r
+\r
+ \r
// Icons : 3 symbols - Column L\r
// Icons : 3 flags - Column M\r
// Icons : 3 symbols 2 - Column N\r
// Mixed icons - Column U\r
\r
}\r
+ private void assertIconSetPercentages(ConditionalFormattingRule cr, IconSet iconset, Double...vals) {\r
+ assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType());\r
+ assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());\r
+ assertEquals(null, cr.getFormula1());\r
+ assertEquals(null, cr.getFormula2());\r
+ \r
+ IconMultiStateFormatting icon = cr.getMultiStateFormatting();\r
+ assertNotNull(icon);\r
+ assertEquals(iconset, icon.getIconSet());\r
+ assertEquals(false, icon.isIconOnly());\r
+ assertEquals(false, icon.isReversed());\r
+ \r
+ assertNotNull(icon.getThresholds());\r
+ assertEquals(vals.length, icon.getThresholds().length);\r
+ for (int i=0; i<vals.length; i++) {\r
+ Double v = vals[i];\r
+ ConditionalFormattingThreshold th = icon.getThresholds()[i];\r
+ assertEquals(RangeType.PERCENT, th.getRangeType());\r
+ assertEquals(v, th.getValue());\r
+ assertEquals(null, th.getFormula());\r
+ }\r
+ }\r
\r
public void testCreateFontFormatting() {\r
Workbook workbook = _testDataProvider.createWorkbook();\r
assertEquals(BorderFormatting.BORDER_HAIR, r1fp.getBorderRight());\r
}\r
\r
- public void testCreateIconFormatting() {\r
- // TODO Implement for XSSF, then test here\r
+ // TODO Fix this test to work for HSSF\r
+ public void DISABLEDtestCreateIconFormatting() {\r
+ Workbook workbook = _testDataProvider.createWorkbook();\r
+ Sheet sheet = workbook.createSheet();\r
+\r
+ SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();\r
+ ConditionalFormattingRule rule1 = \r
+ sheetCF.createConditionalFormattingRule(IconSet.GYRB_4_TRAFFIC_LIGHTS);\r
+ IconMultiStateFormatting iconFmt = rule1.getMultiStateFormatting();\r
+ \r
+ assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());\r
+ assertEquals(4, iconFmt.getThresholds().length);\r
+ assertEquals(false, iconFmt.isIconOnly());\r
+ assertEquals(false, iconFmt.isReversed());\r
+ \r
+ iconFmt.setIconOnly(true);\r
+ iconFmt.getThresholds()[0].setRangeType(RangeType.MIN);\r
+ iconFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);\r
+ iconFmt.getThresholds()[1].setValue(10d);\r
+ iconFmt.getThresholds()[2].setRangeType(RangeType.PERCENT);\r
+ iconFmt.getThresholds()[2].setValue(75d);\r
+ iconFmt.getThresholds()[3].setRangeType(RangeType.MAX);\r
+ \r
+ CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };\r
+ sheetCF.addConditionalFormatting(regions, rule1);\r
+ \r
+ // Save, re-load and re-check\r
+ workbook = _testDataProvider.writeOutAndReadBack(workbook);\r
+ sheetCF = sheet.getSheetConditionalFormatting();\r
+ assertEquals(1, sheetCF.getNumConditionalFormattings());\r
+ \r
+ ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);\r
+ assertEquals(1, cf.getNumberOfRules());\r
+ rule1 = cf.getRule(0);\r
+ iconFmt = rule1.getMultiStateFormatting();\r
+ \r
+ assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());\r
+ assertEquals(4, iconFmt.getThresholds().length);\r
+ assertEquals(true, iconFmt.isIconOnly());\r
+ assertEquals(false, iconFmt.isReversed());\r
+\r
+ assertEquals(RangeType.MIN, iconFmt.getThresholds()[0].getRangeType());\r
+ assertEquals(RangeType.NUMBER, iconFmt.getThresholds()[1].getRangeType());\r
+ assertEquals(RangeType.PERCENT,iconFmt.getThresholds()[2].getRangeType());\r
+ assertEquals(RangeType.MAX, iconFmt.getThresholds()[3].getRangeType());\r
+ assertEquals(null, iconFmt.getThresholds()[0].getValue());\r
+ assertEquals(10d, iconFmt.getThresholds()[1].getValue());\r
+ assertEquals(75d, iconFmt.getThresholds()[2].getValue());\r
+ assertEquals(null, iconFmt.getThresholds()[3].getValue());\r
}\r
\r
public void testBug55380() {\r