Skip to content

Empty <fill></fill> are generated in certain cases #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
brc-dd opened this issue May 2, 2025 · 3 comments
Closed
1 of 2 tasks

Empty <fill></fill> are generated in certain cases #2125

brc-dd opened this issue May 2, 2025 · 3 comments

Comments

@brc-dd
Copy link

brc-dd commented May 2, 2025

Description

In certain cases this is being generated in styles.xml:

  <fills count="3">
    <fill>
      <patternFill patternType="none"></patternFill>
    </fill>
    <fill>
      <patternFill patternType="gray125"></patternFill>
    </fill>
    <fill></fill>
  </fills>
    <xf numFmtId="164" fontId="0" fillId="2" borderId="0" xfId="0" applyNumberFormat="true" applyFill="true" applyAlignment="false">
      <alignment></alignment>
    </xf>

Here, the last <fill></fill> is invalid and causing errors when using strict parsers like openpyxl.

Steps to reproduce the issue

It's bit hard to create a minimal reproduction. The code is part of our internal application. Though the fix is similar to #332

diff --git a/styles.go b/styles.go
index 5992cc3..36c90d0 100644
--- a/styles.go
+++ b/styles.go
@@ -1470,7 +1470,7 @@ func (f *File) extractBorders(bdr *xlsxBorder, s *xlsxStyleSheet, style *Style)
 // extractFills provides a function to extract fill styles settings by
 // given fill styles definition.
 func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
-	if fl != nil {
+	if fl != nil && (fl.GradientFill != nil || fl.PatternFill != nil) {
 		var fill Fill
 		if fl.GradientFill != nil {
 			fill.Type = "gradient"

This is the only place where style.Fill = fill was being assigned without any checks on whether the fill has a gradientfill/patternfill child.

Describe the results you received

<fill></fill> is being generated and fillId="2" is referencing it.

Describe the results you expected

No empty fill should be generated. fillId="0" should be used instead.

Go version

1.24.2

Excelize version or commit ID

2.9.0

Environment

OS: darwin/arm64 – macOS 15.4.1 (24E263)
Microsoft Excel Version: 16.96.1 (25042021)

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.
@xuri
Copy link
Member

xuri commented May 2, 2025

Thanks for your issue. Could you show us a complete, standalone example program or reproducible demo? If you open an existing workbook, please provide the file attachment without confidential info. I'm not sure if this relates to #2014. Please also try using master branch code, upgrade to the master branch code by go get -u github.com/xuri/excelize/v2@master.

@brc-dd
Copy link
Author

brc-dd commented May 2, 2025

Thanks. It works with the latest commit. Sorry for the noise.

@brc-dd brc-dd closed this as completed May 2, 2025
@xuri
Copy link
Member

xuri commented May 2, 2025

Thanks for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants