Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Add background-color to styles #211

Merged
merged 1 commit into from
Aug 10, 2016
Merged

Conversation

madflow
Copy link
Contributor

@madflow madflow commented May 6, 2016

This is loosely related to #182. This PR introduces setting a background color to the styles definition.

  • For xlsx and ods.
  • Only "solid" backgrounds are supported. XLSX supports a whole array of patternType constants (http://www.officeopenxml.com/SSstyles.php, darkGrid, lightTrellis) - these are ignored.
  • XLSX knows a bgColor element (The color to use for the background of the fill when the type is not solid.) and a fgColor element (The color to use for the the background in solid fills.) - these are just treated as the same. Only a sick, twisted minded MS Office Interop Developer knows why a background color in solid fills is a foreColor. (I am kidding of course).
  • Some Tests

@boxcla
Copy link

boxcla commented May 6, 2016

Verified that @madflow has signed the CLA. Thanks for the pull request!

@tegansnyder
Copy link

@madflow you just made my day with this comment:

"Only a sick, twisted minded MS Office Interop Developer knows why a background color in solid fills is a foreColor"

@@ -61,6 +62,13 @@ class Style
/** @var bool Whether the wrap text property was set */
protected $hasSetWrapText = false;

/** @var string Background color */
protected $backgroundColor = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be set to self::DEFAULT_BACKGROUND_COLOR instead of null?

@@ -125,7 +125,7 @@ The writer always generate CSV files encoded in UTF-8, with a BOM.

#### Row styling

It is possible to apply some formatting options to a row. Spout supports fonts as well as alignment styles.
It is possible to apply some formatting options to a row. Spout supports fonts, background as well as alignment styles.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll have to rebase and resolve the conflicts with borders

@madflow
Copy link
Contributor Author

madflow commented Jun 23, 2016

@adrilo Thanks for your support. I rebased and merged the current upstream.

I agree - we should use "best practice / reusing common parts" for the bgColor and fgColor definition and the fills defininition.

This PR does turn out to be more than a quick afterwork hack - so I would appreciate any tips and pushes to the PR branch.

@madflow madflow mentioned this pull request Jul 15, 2016
@madflow
Copy link
Contributor Author

madflow commented Jul 15, 2016

It turns out, XLSX seems to preserve two "no background" definitions. I have a hard time finding the actual specification for this... No luck yet.

But LibreOffice does it and openpyxl and probably everyone else.

    <fills count="2">
        <fill><patternFill patternType="none"/></fill>
        <fill><patternFill patternType="gray125"/></fill>
    </fills>

I'll amend the PR.

I guess I will use fills[0] for "no background" and leave the other one as sleeping beauty.

@adrilo
Copy link
Collaborator

adrilo commented Jul 15, 2016

I don't think there's a spec for that. The spec will describe how <fill> works. Then each implementation does its own thing... Microsoft Excel also creates 2 <fill> so I think we should try to copy that :)

@adrilo
Copy link
Collaborator

adrilo commented Jul 15, 2016

Regarding the reuse of the fills, here is some guidance:

In AbstractStyleHelper.php, when a style gets registered, we should extract the fill information and store it separately. What's stored can be a hash representing the fill data. You assign it an id that you set on the style.
Then every time you register a new style, you do the same thing: extract the fill data, hash it, check if it's already stored; if so, get the id and set it on the style; otherwise add the hash to the fills array with an associated id and set this id to the style.
Finally you write the fills content, you can go through the fills array instead of going through the styles.

This is high level but hopefully it can help. BTW, you can do this in another PR, as the changes involved may be tricky

@madflow
Copy link
Contributor Author

madflow commented Aug 6, 2016

This one is getting very stale :/

I commited and pushed the latest changes. My first approach was to amend the AbstractHelper class - but when I was finished I decided that it should got into the XLSX StyleHelper...

Considering, that fonts and borders are both not re-using definitions in the XLSX world at the moment, this seemed correct. There should be more to come.

  • Wrote better tests
  • Deleted bgColor from the XLSX definition. Openpyxl does not use it when just a "start" color is supplied, so I decided that they have already solved this problem.
  • When we decide to introduce an "end" color - we can introduce it again.
  • Hacked the two default styles by using a seperate counter for the fillIndex.

I am not perfectly happy how it looks at the moment - but it adds imediate business value. (background colors 💃 )

The test code I used for quick copy+paste:
https://gist.github.com/madflow/d134d476f396a7f0753f2bee327008b9

// The other fills are actually registered by setting a background color
foreach ($this->registeredFills as $styleId) {

/** @var $style Style */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be /** @var Style $style */

@adrilo
Copy link
Collaborator

adrilo commented Aug 10, 2016

👍 This looks great!

Now regarding your comments:

Wrote better tests

Feel free to do it now or later. I'm comfortable pushing your changes as is.

Deleted bgColor from the XLSX definition. Openpyxl does not use it when just a "start" color is supplied, so I decided that they have already solved this problem. When we decide to introduce an "end" color - we can introduce it again.

Sounds good!

Hacked the two default styles by using a seperate counter for the fillIndex.

Yeah I guess that's fine. Another way to do it is to store the "pattern" and "fgcolor" attributes in an array inside registeredFills. This way you can add the 2 default fills to this array when it gets initialized and you treat all fills the same way when you render the fills content.

Thanks again for this pull request! I'll merge it whenever you tell me you're done with it.

@madflow
Copy link
Contributor Author

madflow commented Aug 10, 2016

I have pushed the changes, squashed the commits and rebased.

@adrilo
Copy link
Collaborator

adrilo commented Aug 10, 2016

Sorry, I just merged a new commit. I did not see you had pushed a new commit... Can you rebase one more time please?

BTW do you know if I can do it for you? It's annoying to have to ask you every time...

Removed default background color, cosmetics

Remove default background color

reuse bg colors

Cosmetics

Moved reusing fills to XLSX StyleHelper

Tests and inline doc
@madflow
Copy link
Contributor Author

madflow commented Aug 10, 2016

No - problem - I just rebased. I do not think that it is possible as a "Hub" feature. I can give your write access to my fork - but than you would be just doing my work. But - not 100% sure. Would be interesting to know!

@adrilo adrilo merged commit 584121d into box:master Aug 10, 2016
@adrilo
Copy link
Collaborator

adrilo commented Aug 10, 2016

Thanks again @madflow !

@madflow madflow deleted the background-color branch August 24, 2016 20:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants