diff options
author | PJ Fanning <fanningpj@apache.org> | 2024-08-27 18:36:59 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2024-08-27 18:36:59 +0000 |
commit | abf153d6a429a061870cc23636daa3740bd95cc8 (patch) | |
tree | bf9580f3b0736cd2d8b37744ab051efa9eda8130 /poi-ooxml | |
parent | 41453f3916fefd469cfa554dcb5ca069d221b331 (diff) | |
download | poi-abf153d6a429a061870cc23636daa3740bd95cc8.tar.gz poi-abf153d6a429a061870cc23636daa3740bd95cc8.zip |
[github-682] Add CellPropertyType and CellPropertyCategory enums. Thanks to Danila Avdeyenko. This closes #682
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1920230 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
3 files changed, 168 insertions, 8 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 6c468f3c8a..4861a09b5d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -572,7 +572,7 @@ public class SXSSFCell extends CellBase { * the Workbook.</p> * * <p>To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}</p> + * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(Cell, Map)}</p> * * @param style reference contained in the workbook. * If the value is null then the style information is removed causing the cell to used the default workbook style. diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 03110ec00b..20b7a2cbf4 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -606,7 +606,7 @@ public final class XSSFCell extends CellBase { * the XSSFWorkbook.</p> * * <p>To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, java.util.Map)}</p> + * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(Cell, java.util.Map)}</p> * * @param style reference contained in the workbook. * If the value is null then the style information is removed causing the cell to use the default workbook style. diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java index 58b93c6047..1597524183 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.tests.util; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellPropertyType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; @@ -32,13 +33,14 @@ import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.Test; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.util.LinkedHashMap; import java.util.Map; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; class TestXSSFCellUtil extends BaseTestCellUtil { public TestXSSFCellUtil() { @@ -46,6 +48,24 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } @Test + public void testSetForegroundColorCellStylePropertyByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("AAAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + } + } + + @Test public void testSetForegroundColorCellStyleProperty() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { @@ -64,8 +84,32 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } @Test - public void testSetForegroundColorCellStylePropertyToNull() throws IOException, DecoderException { + public void testSetForegroundColorCellStylePropertyToNullByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("AAAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + + assertNotEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor()); + } + } + @Test + public void testSetForegroundColorCellStylePropertyToNull() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { final Sheet sheet = workbook.createSheet("Sheet"); @@ -90,6 +134,41 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } @Test + public void testSetForegroundColorCellStylePropertiesToNullByEnum() throws IOException, DecoderException { + + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FF0000")); + + { + final Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(FillPatternType.SOLID_FOREGROUND, cell.getCellStyle().getFillPattern()); + + { + final Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor()); + assertEquals(FillPatternType.NO_FILL, cell.getCellStyle().getFillPattern()); + } + } + + @Test public void testSetForegroundColorCellStylePropertiesToNull() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { @@ -124,9 +203,49 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } } + @Test - public void testBug66052WithWorkaround() throws IOException, DecoderException { + public void testBug66052WithWorkaroundByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, null); // WORKAROUND + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNotNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, null); // WORKAROUND + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + } + } + @Test + public void testBug66052WithWorkaround() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { final Sheet sheet = workbook.createSheet("Sheet"); @@ -166,6 +285,47 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } @Test + public void testBug66052WithoutWorkaroundByEnum() throws IOException, DecoderException { + + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), + ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); + + { + Map<CellPropertyType, Object> properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), + ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); + } + } + + @Test public void testBug66052WithoutWorkaround() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { @@ -205,4 +365,4 @@ class TestXSSFCellUtil extends BaseTestCellUtil { ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); } } -}
\ No newline at end of file +} |