jnthn / spreadsheet-xlsx

Work with Excel (XLSX) spreadsheets in Raku.
15 stars 10 forks source link

Implement reading of styles and more #19

Closed vrurg closed 1 year ago

vrurg commented 1 year ago

Also added support for colors, gradient fills, and cell styles.

More importantly, standard support for XML (de)serialization has been implemented. In most cases adding a new data type could now be as simple as:

class XMLRecord {
    has $.attr1 is xml-attr;
    has $.attr2 is xml-elem;
}
vrurg commented 1 year ago

Unless there are any remarks on this PR and on #18 I'd be happy to see a new module release ASAP as my work depends on these.

vrurg commented 1 year ago

Turns out I forgot about styles on cells and it requires some more work.

jnthn commented 1 year ago

@vrurg OK, ping me again when it's ready for review. Was away last week on a work trip with no time to look at anything...

jnthn commented 1 year ago

I can cut a release after it's in.

vrurg commented 1 year ago

Ping @jnthn. It's just crazy how much had to be changed to get a seemingly simple task done! But partly it was caused by me couldn't resist implementing style re-use. And even more as now setting cell number format to 0% would result in number format ID set to the built-in 9.

I tried to preserve backward compatibility, but decided that SharedStrings returning a Cell is wrong. So, it returns CT_Rst now. Hope it's not a big deal as it is barely something to be often used by end-users.

Spreadsheet::XLSX::CellStyle now has full support for cell format record as such (Spreadsheet::XLSX::Styles::Format), font, border, fill, alignment, and number format. Aside of some shortcut methods, exported via handles trait, all attributes are accessible and changeable directly via $cell.style.<group>.<property>:

    $cell.style.font.name = "Andale Mono";
    $cell.style.number-format = '$###.##';
    say $cell.style.bold;
    if $cell.style.use-fill && $cell.style.fill.of ~~ PatternFill { ... }

Any style changes are not reflected until sync-style-id method is invoked.

These are the most visible changes, perhaps.

Tested on a real-life file I have and everything seems to be working. I still have to test a formatted cell content which is likely to fail, but so far I'm not ready to get into it. Perhaps would something something later, if anything pops up.

vrurg commented 1 year ago

One last issue I have found is that support for formatted shared strings is incomplete. I had no example to test against until today. The structure is pulled in correctly, it's just about flattening into a string and providing access to the original structure from the cell itself.

vrurg commented 1 year ago

Now it is all done and ready for use. I wash my hands! :)

patrickbkr commented 1 year ago

Argh! We started working on spreadsheet::XLSX simultaneously. I only just noticed after uploading my PRs. I vote to have this PR merged first. I'll see if there is anything worth resurrecting in my PRs afterwards.

vrurg commented 1 year ago

@patrickbkr didn't have time to look into the iterator thing of yours and what's the purpose of it. But re-implementing Cells with xml-sequence trait would already give you an iterator over existing elements. It is not very smart, but certainly lazy as XMLSequence role is vivifying elements on demand.