Skip to content
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

When I do not set the conditional formatting style, I will get an nil error #1748

Closed
ShowerBandV opened this issue Dec 11, 2023 · 4 comments
Closed
Labels
bug Something isn't working
Projects

Comments

@ShowerBandV
Copy link
Contributor

Description
Reading a file with conditional formatting as cell values, but without formatting, GetConditionalFormats() will return a nil error. It was found that the issue was caused by DxfID being nil. We hope to fix this issue

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/xuri/excelize/v2"
)

func main() {
	file, err := excelize.OpenFile("bbb.xlsx")
	if err != nil {
		fmt.Printf("open file failed:%s", err.Error())
		return
	}
	formats, err := file.GetConditionalFormats("Sheet1")
	if err != nil {
		fmt.Printf("get conditionformats failed:%s", err.Error())
		return
	}
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x10bef2b]

goroutine 1 [running]:
github.com/xuri/excelize/v2.extractCondFmtCellIs(_, _)
        D:/Users/xx/go/pkg/mod/github.com/xuri/excelize/[email protected]/styles.go:2764 +0xcb 
github.com/xuri/excelize/v2.(*File).GetConditionalFormats(0x11625d7?, {0x115eb16, 0x6})
        D:/Users/xx/go/pkg/mod/github.com/xuri/excelize/[email protected]/styles.go:2996 +0x2a5
main.main()
        E:/projects/excelizeDemo/main.go:17 +0x65

And I have found that errors can also occur if the conditional format type is null and not formatted. I hope you can handle similar unformatted scenarios.

@ShowerBandV
Copy link
Contributor Author

The excel file is below:
bbb.xlsx

@xuri
Copy link
Member

xuri commented Dec 11, 2023

Thanks for your issue. This issue already has been fixed by commit 866f308. Please upgrade to the master branch code, and this patch will be released in the next version. I've closed this, if it still not work, please let me know, and you can reopen this anytime.

@xuri xuri closed this as completed Dec 11, 2023
@xuri xuri added the bug Something isn't working label Dec 11, 2023
@xuri xuri added this to Bugfix in v2.8.1 Dec 11, 2023
@ShowerBandV
Copy link
Contributor Author

ShowerBandV commented Mar 15, 2024

@xuri I found something wrong in this commit, if there is a condition A with style ,the unstyled condition will be applied with A's style.

func (f *File) extractCondFmtCellIs(c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions {
   format := ConditionalFormatOptions{StopIfTrue: c.StopIfTrue, Type: "cell", Criteria: operatorType[c.Operator]}
   if c.DxfID != nil {
   	format.Format = *c.DxfID
   }
   if len(c.Formula) == 2 {
   	format.MinValue, format.MaxValue = c.Formula[0], c.Formula[1]
   	return format
   }
   format.Value = c.Formula[0]
   return format
}

because the condition A's DxfID is 0,that is to say c.DxfID is 0 , and the unstyled condition's DxfID is nil, format default is 0,so after extract,they all will be 0,we can't distinguish who is unstyled

@xuri
Copy link
Member

xuri commented Apr 3, 2024

@xuri I found something wrong in this commit, if there is a condition A with style ,the unstyled condition will be applied with A's style.

func (f *File) extractCondFmtCellIs(c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions {
   format := ConditionalFormatOptions{StopIfTrue: c.StopIfTrue, Type: "cell", Criteria: operatorType[c.Operator]}
   if c.DxfID != nil {
   	format.Format = *c.DxfID
   }
   if len(c.Formula) == 2 {
   	format.MinValue, format.MaxValue = c.Formula[0], c.Formula[1]
   	return format
   }
   format.Value = c.Formula[0]
   return format
}

because the condition A's DxfID is 0,that is to say c.DxfID is 0 , and the unstyled condition's DxfID is nil, format default is 0,so after extract,they all will be 0,we can't distinguish who is unstyled

Thanks for the feedback. This same as issue #1867. I've fixed it. Please upgrade to the master branch code, and this breaking change will be released in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
v2.8.1
Bugfix
Development

No branches or pull requests

2 participants