diff --git a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java index fa01148cea..7358edf682 100644 --- a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java +++ b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java @@ -57,8 +57,7 @@ public class ConditionalFormats { shadeBands(wb.createSheet("Shade Bands")); iconSets(wb.createSheet("Icon Sets")); colourScales(wb.createSheet("Colour Scales")); - - // TODO Add data bars, see bug #58130 + dataBars(wb.createSheet("Data Bars")); // Write the output to a file String file = "cf-poi.xls"; @@ -541,4 +540,69 @@ public class ConditionalFormats { ((ExtendedColor)cs3.getColors()[1]).setARGBHex("FF63BE7B"); sheetCF.addConditionalFormatting(regions, rule3); } + + /** + * DataBars / Data-Bars allow you to have bars shown vary + * based on the values, from full to empty + */ + static void dataBars(Sheet sheet) { + sheet.createRow(0).createCell(0).setCellValue("Data Bars"); + Row r = sheet.createRow(1); + r.createCell(1).setCellValue("Green Positive"); + r.createCell(2).setCellValue("Blue Mix"); + r.createCell(3).setCellValue("Red Negative"); + r = sheet.createRow(2); + r.createCell(1).setCellValue(0); + r.createCell(2).setCellValue(0); + r.createCell(3).setCellValue(0); + r = sheet.createRow(3); + r.createCell(1).setCellValue(5); + r.createCell(2).setCellValue(-5); + r.createCell(3).setCellValue(-5); + r = sheet.createRow(4); + r.createCell(1).setCellValue(10); + r.createCell(2).setCellValue(10); + r.createCell(3).setCellValue(-10); + r = sheet.createRow(5); + r.createCell(1).setCellValue(5); + r.createCell(2).setCellValue(5); + r.createCell(3).setCellValue(-5); + r = sheet.createRow(6); + r.createCell(1).setCellValue(20); + r.createCell(2).setCellValue(-10); + r.createCell(3).setCellValue(-20); + sheet.setColumnWidth(0, 3000); + sheet.setColumnWidth(1, 5000); + sheet.setColumnWidth(2, 5000); + sheet.setColumnWidth(3, 5000); + + SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting(); + + ExtendedColor color = sheet.getWorkbook().getCreationHelper().createExtendedColor(); + color.setARGBHex("FF63BE7B"); + CellRangeAddress[] regions = { CellRangeAddress.valueOf("B2:B7") }; + ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(color); + DataBarFormatting db1 = rule1.getDataBarFormatting(); + db1.getMinThreshold().setRangeType(RangeType.MIN); + db1.getMaxThreshold().setRangeType(RangeType.MAX); + sheetCF.addConditionalFormatting(regions, rule1); + + color = sheet.getWorkbook().getCreationHelper().createExtendedColor(); + color.setARGBHex("FF5A8AC6"); + regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C2:C7") }; + ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(color); + DataBarFormatting db2 = rule2.getDataBarFormatting(); + db2.getMinThreshold().setRangeType(RangeType.MIN); + db2.getMaxThreshold().setRangeType(RangeType.MAX); + sheetCF.addConditionalFormatting(regions, rule2); + + color = sheet.getWorkbook().getCreationHelper().createExtendedColor(); + color.setARGBHex("FFF8696B"); + regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D2:D7") }; + ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(color); + DataBarFormatting db3 = rule3.getDataBarFormatting(); + db3.getMinThreshold().setRangeType(RangeType.MIN); + db3.getMaxThreshold().setRangeType(RangeType.MAX); + sheetCF.addConditionalFormatting(regions, rule3); + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java index faee42b83f..6dc1bcfa88 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java @@ -17,6 +17,7 @@ package org.apache.poi.hssf.usermodel; +import org.apache.poi.hssf.record.common.ExtendedColor; import org.apache.poi.ss.usermodel.CreationHelper; public class HSSFCreationHelper implements CreationHelper { @@ -42,7 +43,11 @@ public class HSSFCreationHelper implements CreationHelper { return new HSSFHyperlink(type); } - /** + public HSSFExtendedColor createExtendedColor() { + return new HSSFExtendedColor(new ExtendedColor()); + } + + /** * Creates a HSSFFormulaEvaluator, the object that evaluates formula cells. * * @return a HSSFFormulaEvaluator instance diff --git a/src/java/org/apache/poi/ss/usermodel/CreationHelper.java b/src/java/org/apache/poi/ss/usermodel/CreationHelper.java index c073237232..893ce41ec2 100644 --- a/src/java/org/apache/poi/ss/usermodel/CreationHelper.java +++ b/src/java/org/apache/poi/ss/usermodel/CreationHelper.java @@ -51,6 +51,12 @@ public interface CreationHelper { * @return a FormulaEvaluator instance */ FormulaEvaluator createFormulaEvaluator(); + + /** + * Creates a XSSF-style Color object, used for extended sheet + * formattings and conditional formattings + */ + ExtendedColor createExtendedColor(); ClientAnchor createClientAnchor(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java index 390c20fa91..154fec6ff4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java @@ -39,6 +39,10 @@ public class XSSFCreationHelper implements CreationHelper { return workbook.createDataFormat(); } + public XSSFColor createExtendedColor() { + return new XSSFColor(); + } + /** * Create a new XSSFHyperlink. * diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index d14b556f44..a12dbc13d9 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -1138,7 +1138,50 @@ public abstract class BaseTestConditionalFormatting extends TestCase { } public void testCreateDataBarFormatting() { - // TODO Implement then test + Workbook workbook = _testDataProvider.createWorkbook(); + Sheet sheet = workbook.createSheet(); + + String colorHex = "FFFFEB84"; + ExtendedColor color = workbook.getCreationHelper().createExtendedColor(); + color.setARGBHex(colorHex); + SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting(); + ConditionalFormattingRule rule1 = + sheetCF.createConditionalFormattingRule(color); + DataBarFormatting dbFmt = rule1.getDataBarFormatting(); + + assertEquals(false, dbFmt.isIconOnly()); + assertEquals(true, dbFmt.isLeftToRight()); + assertEquals(0, dbFmt.getWidthMin()); + assertEquals(100, dbFmt.getWidthMax()); + assertColour(colorHex, dbFmt.getColor()); + + dbFmt.getMinThreshold().setRangeType(RangeType.MIN); + dbFmt.getMaxThreshold().setRangeType(RangeType.MAX); + + CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") }; + sheetCF.addConditionalFormatting(regions, rule1); + + // Save, re-load and re-check + workbook = _testDataProvider.writeOutAndReadBack(workbook); + sheetCF = sheet.getSheetConditionalFormatting(); + assertEquals(1, sheetCF.getNumConditionalFormattings()); + + ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0); + assertEquals(1, cf.getNumberOfRules()); + rule1 = cf.getRule(0); + dbFmt = rule1.getDataBarFormatting(); + assertEquals(ConditionType.DATA_BAR, rule1.getConditionTypeType()); + + assertEquals(false, dbFmt.isIconOnly()); + assertEquals(true, dbFmt.isLeftToRight()); + assertEquals(0, dbFmt.getWidthMin()); + assertEquals(100, dbFmt.getWidthMax()); + assertColour(colorHex, dbFmt.getColor()); + + assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType()); + assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType()); + assertEquals(null, dbFmt.getMinThreshold().getValue()); + assertEquals(null, dbFmt.getMaxThreshold().getValue()); } public void testBug55380() {