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

SaveAs(), unprotects all protected/locked cells #809

Closed
JDavidVR opened this issue Mar 24, 2021 · 5 comments
Closed

SaveAs(), unprotects all protected/locked cells #809

JDavidVR opened this issue Mar 24, 2021 · 5 comments
Projects

Comments

@JDavidVR
Copy link
Contributor

JDavidVR commented Mar 24, 2021

Description

If I create a new workbook, and set C3 and C4 cells as Hidden and Protected/Locked, when I save it as a new file using excelize 2.3.2, it removes the formula protection.

image

Test Excel: NewBook.xlsx

Steps to reproduce the issue:

func copy() {
	f, err := excelize.OpenFile("NewBook.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	if err := f.SaveAs("goNewBook.xlsx"); err != nil {
		fmt.Println(err)
	}
}

Describe the results you received:
C4 and C5 should remain Hidden BUT NOT Locked:
image

Describe the results you expected:
C4 and C5 should remain Hidden and Locked:
image

Environment details (OS, Microsoft Excel™ version, physical, etc.):
Mac OS Big Sur 11.2.2
go version go1.16.2 darwin/amd64

I did some investagion, and looks like issue occurs in xl/styles.xml

Excel 2010's xl/styles.xml:

<cellXfs count="2">
        <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
        <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyProtection="1">
            <protection hidden="1"/>
        </xf>
    </cellXfs>

Excelize 2.3.2's xl/styles.xml:

<cellXfs count="2">
        <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"></xf>
        <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyProtection="true">
            <protection hidden="true" locked="false"></protection>
        </xf>
    </cellXfs>

Excelize by default is adding locked="false" and MS Excel is not adding it.

Reviewing code https://github.com/360EntSecGroup-Skylar/excelize/blob/ab2c1c8fe1ef787c1f692bcf26c7bbe0cca2c5b3/xmlStyles.go#L53

looks like struct have a bool value, and like it is not especofied, go will set false by default

type xlsxProtection struct {
	Hidden bool `xml:"hidden,attr"`
	Locked bool `xml:"locked,attr"`
}

I would like to help solving this issue, but I new in go and also I've started reviewing excelize code, so if you metnion which files needs to be modified and how to debug I can help resolving this issue.

Thanks in advance

@JDavidVR JDavidVR changed the title SaveAs() unpretects, all protected cells SaveAs() unpretects, all protected/locked cells Mar 24, 2021
@JDavidVR JDavidVR changed the title SaveAs() unpretects, all protected/locked cells SaveAs(), unprotects all protected/locked cells Mar 24, 2021
@xuri
Copy link
Member

xuri commented Mar 24, 2021

Thanks for your issue, I propose to change the struct Protection like this:

type xlsxProtection struct {
    Locked *bool `xml:"locked,attr"`
    Hidden *bool `xml:"hidden,attr"`
}

@JDavidVR
Copy link
Contributor Author

Hi @xuri

I've applied the change you've suggested:
image

But IDE is displaying this alert:
image

In terminal I'm getting this same message too:

/Users/david/go/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/v2@v2.3.2/styles.go:2475:21: cannot use style.Protection.Hidden (type bool) as type *bool in assignment
/Users/david/go/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/v2@v2.3.2/styles.go:2476:21: cannot use style.Protection.Locked (type bool) as type *bool in assignment

xuri added a commit that referenced this issue Mar 24, 2021
@xuri
Copy link
Member

xuri commented Mar 24, 2021

I have fixed it, please try to use the master branch code, and this patch will be released in the next version.

@xuri xuri closed this as completed Mar 24, 2021
@JDavidVR
Copy link
Contributor Author

Hi @xuri thanks I can confirm, this is working well!

@arthurwailer
Copy link

i have the same problem, how resolved ?

\github.com\xuri\excelize\v2@v2.8.0\styles.go:1297:30: cannot use *xf.Protection.Hidden (variable of type bool) as *bool value in assignment

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v2.4.0
Awaiting triage
Development

No branches or pull requests

3 participants