rabanti-github / PicoXLSX

PicoXLSX is a small .NET / C# library to create XLSX files (Microsoft Excel 2007 or newer) in an easy and native way
MIT License
52 stars 13 forks source link

Individual Cell Styles #14

Closed AK5nowman closed 3 years ago

AK5nowman commented 3 years ago

Hello,

Appreciate the library. I'm having an issue and I'm not sure if its just my lack of understanding or if its perhaps a bug.

When I run the following code (hand typed here, so ignore syntactical errors) I would have expected that my row of data to have a gray background except for the cell in column 2 having a red background.

workbook.CurrentWorksheet.AddCellRange(data, new Cell.Address(0, idx), new Cell.Address(data.Count - 1, idx), Style.BasicStyles.ColorizedBackground("a6a1a1"));
workbook.CurrentWorksheet.GetCell(2, idx).SetStyle(Style.BasicStyles.ColorizedBackground("bf6262"));

It currently changes the style for the whole row to have the red background. I though perhaps it was the fact that I was adding a range of data instead of a single cell at a time so I tried this as well, with the same result:

data.ForEach(p=> workbook.CurrentWorksheet.AddNextCell(p, Style.BasicStyles.ColorizedBackground("a6a1a1")));
workbook.CurrentWorksheet.GetCell(2, 0).SetStyle(Style.BasicStyles.ColorizedBackground("bf6262"));

Would you expect it to change the style of the whole row or am I doing something wrong?

rabanti-github commented 3 years ago

Hi Thank you for the report. I will have a look at this ASAP.

But it could be that we have a reference problem here (probably a bug) with the Basic Styles. The styles are stored as references not values, and redefining BasicStyles.ColorizedBackground may override all references in the cells. As quick fix, you could try to use the copy function of the style class to create two distinct styles instead of of using BasicStyle.ColorizedBackground directly.

But like said, I will look into it.

AK5nowman commented 3 years ago

Gave that a quick test, same results

Style evenStyle = new Style().CurrentFill.BackgroundColor = "a6a1a1";
Style diffStyle = new Style();
diffStyle.CurrentBorder.BottomStyle = Style.Border.StyleValue.medium;
diffStyle.CurrentBorder.BottomColor = "bf6262";

workbook.CurrentWorksheet.AddCellRange(data, new Cell.Address(0, idx), new Cell.Address(data.Count - 1, idx), evenStyle);
workbook.CurrentWorksheet.GetCell(2, idx).SetStyle(diffStyle);
AK5nowman commented 3 years ago

Mighty embarrassing... I had a variable mistyped in some supporting code that my breakpoints weren't capturing.

I apologize for wasting your time. Thank you for the repo!

rabanti-github commented 3 years ago

No Problem. I know that the style section is quite complicated und not very well documented. A rewriting is ongoing since some months. But it will take probably some additional months until it can be released. Please let me know if you find inconveniences, problems or bugs. Thank you.

AK5nowman commented 3 years ago

So I'm doing this below:

Style style1 = new Style();
style1.CurrentFill.BackgroundColor = "00ff00";
Style style2 = new Style();

And then I write some cells with an explicit style of either default or style1 or style2. The cells I'm expecting to have a colored background, because of style1, do not.