You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2022/05/06 05:20:47 UTC

[Bug 66052] New: XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

            Bug ID: 66052
           Summary: XSSFColor cannot be used the same time as
                    org.apache.poi.ss.util classes
           Product: POI
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: axel.richter.privat@web.de
  Target Milestone: ---

If one tries using XSSFCellStyle having XSSFColor as interior fill color and
then tries using org.apache.poi.ss.util classes (PropertyTemplate or CellUtil
or RegionUtil) then after this all cell interiors are filled black, because
IndexedColors 0 is set for the interior.

That is because org.apache.poi.ss.util classes do not take into account the RGB
interior cell fill and org.apache.poi.xssf.usermodel.IndexedColorMap has no
meaningful usage until now.

Complete example to test:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
import org.apache.poi.xssf.usermodel.*;
import org.apache.commons.codec.binary.Hex;

class SSUtilVsXSSFColor {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new
FileOutputStream("./SSUtilVsXSSFColor.xlsx") ) {

   CellStyle cellStyle = workbook.createCellStyle();
   if (cellStyle instanceof XSSFCellStyle) {
    XSSFCellStyle xssfCellStyle = (XSSFCellStyle)cellStyle;
        XSSFWorkbook xssfWorkbook = (XSSFWorkbook)workbook;
    String rgbS = "FFFF00";
    byte[] rgbB = Hex.decodeHex(rgbS);
        IndexedColorMap colorMap =
xssfWorkbook.getStylesSource().getIndexedColors();
    XSSFColor color = new XSSFColor(rgbB, colorMap);
    xssfCellStyle.setFillForegroundColor(color);
    xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);        
   }

   int startDataRow = 6; // row 7 (index 0-based)
   int endDataRow = 11; // row 12 (index 0-based)
   int startDataColumn = 1; // column B (index 0-based)
   int endDataColumn = 10; // column K (index 0-based)

   Sheet sheet = workbook.createSheet();

   for (int r = startDataRow; r <= endDataRow; r++) {
    Row row = sheet.createRow(r);
    for (int c = startDataColumn; c <= endDataColumn; c++) {
     Cell cell = row.createCell(c);
     cell.setCellValue(cell.getAddress().formatAsString());
         cell.setCellStyle(cellStyle);
    }
   }

   PropertyTemplate propertyTemplate = new PropertyTemplate();
   propertyTemplate.drawBorders(new CellRangeAddress(startDataRow, endDataRow,
startDataColumn, endDataColumn), 
    BorderStyle.MEDIUM, BorderExtent.ALL);

   propertyTemplate.applyBorders(sheet); // after this all cell interiors are
filled black, because IndexedColors 0 is set 
   // same is usimg all other org.apache.poi.ss.util classes which manipulate
cell styles (CellUtil or RegionUtil) 

   workbook.write(fileout);

  }
 }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #19 from PJ Fanning <fa...@yahoo.com> ---
The new code here isn't even released. You'd be much better coding something
yourself to update the styles.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #7 from PJ Fanning <fa...@yahoo.com> ---
I needed to add r1902626 - there are 2 tests that I need to investigate so I
have temporarily disabled them

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #23 from PJ Fanning <fa...@yahoo.com> ---
The code already does this:

        if (foregroundFillColor != null) {
            try {
                style.setFillForegroundColor(foregroundFillColor);
            } catch (IllegalArgumentException iae) {
                LOGGER.atDebug().log("Mismatched FillForegroundColor instance
used", iae);
            }
        }

So if you try to set a XSSF style on a HSSF workbook (or HSSF style on an XSSF
workbook), the failure is caught and logged. This is useful for allowing HSSF
cell data to be copied to an XSSF target (or XSSF cell to a HSSF target).

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #20 from XL <xl...@ijoinery.com> ---
The issue cause is in CellUtil.setFormatProperties in this condition :
if (foregroundFillColor != null)

I suggest to replace the previous condition with this one :
if (style instanceof XSSFCellStyle)

After this replacement :
• The JUnit test of comment 13 works completely,
• And the issue of comment 15 disappears.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

PJ Fanning <fa...@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from PJ Fanning <fa...@yahoo.com> ---
added test case using r1902637

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #14 from PJ Fanning <fa...@yahoo.com> ---
I added r1903828

After you set the fill color to null, there were still be an XSSF color
associated with the style but this color will not be the same as before. It
will be based based on IndexedColors.AUTOMATIC. Get back to me if this is an
issue for you.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #6 from PJ Fanning <fa...@yahoo.com> ---
added r1902623

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #12 from PJ Fanning <fa...@yahoo.com> ---
Ok - I had a quick go at fixing this and r1903816 seems to help. This code can
be very messy but you can change colours using short values too - eg
CellUtil.FILL_FOREGROUND_COLOR, shortValue)

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #10 from XL <xl...@ijoinery.com> ---
When the CellUtil class of r1902633 is used,
the following issue occurs.

If a cell style has a fill foreground color that is initially null,
then the color remains null after calling CellUtil.setCellStyleProperty.

The error cause is in CellUtil.styleMapsMatch in this expression :
final boolean foreColorsMatch =
  foreColor1 == null || foreColor2 == null || foreColor1.equals(foreColor2);

The previous expression can be corrected as follows :
final boolean foreColorsMatch =
  Objects.equals(foreColor1, foreColor2);

The issue can be reproduced by executing the following JUnit test,
in which the assertNotNull call throws an error.




package test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class CellUtilTest {

  @Test
  public void testSetCellStyleProperty() 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, CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
    }
  }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #22 from Nick Burch <ap...@gagravarr.org> ---
Could you in effect test for XSSFColor via
org.apache.poi.ss.usermodel.ExtendedColor ? IIRC it's only a few conditional
formatting bits of HSSF that use those, the regular HSSFColor class doesn't

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #21 from PJ Fanning <fa...@yahoo.com> ---
that change cannot be made as CellUtil is in a different jar (poi.jar) from the
XSSF classes (poi-ooxml.jar) - and we won't introduce a circular jar dependency
between these 2 jars

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #18 from XL <xl...@ijoinery.com> ---
Thank you very much for your help.

I use the CellUtil class
because it prevents the creation of unnecessary styles
by automatically reusing existing ones.

Is it correct/incorrect to proceed in this way ?

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #15 from XL <xl...@ijoinery.com> ---
The following issue occurs,
when using the CellUtil class of r1903828.

The issue can be reproduced as follows :
1. Execute the program below, which generates 2 xlsx files.
2. Using the Excel application, open this xlsx file :
BeforeSettingNullFill.xlsx
3. Check that the upper-left cell has a red background.
4. Using the Excel application, open this xlsx file : AfterSettingNullFill.xlsx
5. Check that the upper-left cell has a white background.
6. Double-click in the upper-left cell.
7. Notice that the cell has unexpectedly a black background.
8. Type some text into the upper-left cell.
9. Notice that the text is unexpectedly unreadable because of the background.




package test;

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.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

public class CellUtilProgram {

  public static void main(String[] args) 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<String, Object> properties =
          new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
        properties.put(CellUtil.FILL_PATTERN,
FillPatternType.SOLID_FOREGROUND);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      try (OutputStream stream =
        new FileOutputStream("BeforeSettingNullFill.xlsx")) {

        workbook.write(stream);
      }

      {
        final Map<String, Object> properties =
          new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
        properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      try (OutputStream stream =
        new FileOutputStream("AfterSettingNullFill.xlsx")) {

        workbook.write(stream);
      }
    }
  }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #16 from PJ Fanning <fa...@yahoo.com> ---
Is there a reason to use CellUtil for these changes? Why not use the Cell and
CellStyle methods for changing styles directly?

The CellUtil code was written to support copying cells from one sheet to
another and its internal use of maps make it difficult to support changes to
null values.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

PJ Fanning <fa...@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |59442


Referenced Bugs:

https://bz.apache.org/bugzilla/show_bug.cgi?id=59442
[Bug 59442] CellUtil.setCellStyleProperties erases foreground and background
fill colors
-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #3 from Axel Richter <ax...@web.de> ---
(In reply to Axel Richter from comment #2)
> Created attachment 38338 [details]
> patches for CellStyle, HSSFCellStyle, XSSFCellStyle and CellUtil

Patches would be need for org.apache.poi.ss.usermodel.CellStyle,
org.apache.poi.hssf.usermodel.HSSFCelStyle,
org.apache.poi.xssf.usermodel.XSSFCellStyle and
org.apache.poi.ss.util.CellUtil. I have attached tested versions of
CellStyle.java, HSSFCelStyle.java, XSSFCellStyle.java and CellUtil.java here.
Added code is marked with comment //PATCHED:. Hope it helps.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #4 from PJ Fanning <fa...@yahoo.com> ---
Thanks very much Axel. I applied the changes with r1902622. I had to make a few
changes because the patch files seem not to have been based on the latest code.
Feel free to review the diff on my commit to see if I missed anything.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #11 from PJ Fanning <fa...@yahoo.com> ---
Thanks - your change breaks a number of tests so I didn't uptake your change. I
added the test but disabled it. r1903815

I don't have time right now to debug the issue but I won't merge any changes
that break existing tests.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #8 from PJ Fanning <fa...@yahoo.com> ---
I added r1902630 because I'm worried that making the color set methods too
strict could affect users who want to use CellUtil to copy between XSSF and
HSSF workbooks (or vice versa) and allow this to be best effort (because not
everything will copy over)>

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #24 from PJ Fanning <fa...@yahoo.com> ---
I added more changes to CellUtil in last 24 hours, so maybe you could try the
latest changes.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #28 from PJ Fanning <fa...@yahoo.com> ---
I added the tests but modified the 2nd test so it passes - r1903883

I didn't add the code that treats null colors as
org.apache.poi.ss.usermodel.IndexedColors#AUTOMATIC - and depending on code
paths, the null colors can be replaced by non-null colors that are based on
org.apache.poi.ss.usermodel.IndexedColors#AUTOMATIC.

I'm not interested in changing this - for backward compatibility and other
reasons.

As far as I can see, the tests you just added still produce xlsx files that
look ok.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #29 from XL <xl...@ijoinery.com> ---
I would like to clarify my previous comment 27
about testing CellUtil of r1903837.

If CellUtilTest.testWithoutWorkaround fails,
then the Excel issue of comment 15 occurs.

If CellUtilTest.testWithoutWorkaround passed,
then the Excel issue of comment 15 would disappear.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #5 from Nick Burch <ap...@gagravarr.org> ---
I wonder if we should throw an exception if the wrong kind of color is passed
in (hssfcolor to xssfcellstyle and vice versa), similar to how we do in
cloneStyleFrom(CellStyle source) ?

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #1 from Nick Burch <ap...@gagravarr.org> ---
I don't know the code well, but from a brief look, I think the issue is in
CellStyle.getFormatProperties and CellStyle.setFormatProperties both only using
the indexed colours

We do have CellStyle.getFillBackgroundColorColor and CellStyle.        
getFillForegroundColorColor but no matching setters on the interface that take
a Color. HSSFCellStyle doesn't have ones taking a Color, while XSSFCellStyle
has ones taking a XSSFColor but nor a Color

I'm not certain, but I think adding setters that take a Color onto the
interface and implementing that on HSSFCellStyle (with an error there if not an
indexed one), then updating CellStyle to optionally work with a Color instead
of a short, might be enough

Patches welcome! :)

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #2 from Axel Richter <ax...@web.de> ---
Created attachment 38338
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=38338&action=edit
patches for CellStyle, HSSFCellStyle, XSSFCellStyle and CellUtil

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #17 from PJ Fanning <fa...@yahoo.com> ---
POI is a volunteer project and I don't have time or inclination to fix
everything. PRs are accepted but please don't break existing tests - or if you
have to break them, you need to justify why you need to change the behaviour in
that way.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #13 from XL <xl...@ijoinery.com> ---
Thank you very much for your correction in r1903816.
I confirm that my previous JUnit test now works.

But when the CellUtil class of r1903816 is used,
the new following issue occurs.

If a cell style has a fill foreground color that is initially NOT null,
then the color remains NOT null after calling CellUtil.setCellStyleProperty.

The issue can be reproduced by executing the new following JUnit test,
in which the last assertNull call throws an error.



package test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class CellUtilTest {

  @Test
  public void testSetCellStyleProperty() 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, CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());

      CellUtil.setCellStyleProperty(
        cell, CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
    }
  }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #26 from PJ Fanning <fa...@yahoo.com> ---
That revision is a good one to test but you could just take the latest
revision.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #25 from XL <xl...@ijoinery.com> ---
Yes, I can test the changes of the CellUtil class.

Could you confirm that the revision to be tested is r1903837 ?

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 66052] XSSFColor cannot be used the same time as org.apache.poi.ss.util classes

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #27 from XL <xl...@ijoinery.com> ---
I have further tested the CellUtil class,
and I have noticed the following behaviour.

If I set the fill foreground color,
and I do not set the fill background color,
then the fill background color gets not null unexpectedly.

If I set the fill foreground color,
and I set the fill background color to null as a workaround,
then the fill background color remains null as expected.

Please find below a new JUnit test,
which allows to reproduce this issue,
and which covers the issues of comment 13 and comment 15.

When testing the CellUtil class of r1903837 :
• CellUtilTest.testWithWorkaround  passes.
• CellUtilTest.testWithoutWorkaround  fails.







package test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

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.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class CellUtilTest {

  @Test
  public void testWithWorkaround() 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<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
        properties.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null); //
WORKAROUND
        properties.put(CellUtil.FILL_PATTERN,
FillPatternType.SOLID_FOREGROUND);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
        properties.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null); //
WORKAROUND
        properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());
    }
  }

  @Test
  public void testWithoutWorkaround() 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<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
        properties.put(CellUtil.FILL_PATTERN,
FillPatternType.SOLID_FOREGROUND);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
        properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());
    }
  }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org