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

File corruption after re-saving #1323

Closed
romanzh1 opened this issue Aug 19, 2022 · 4 comments
Closed

File corruption after re-saving #1323

romanzh1 opened this issue Aug 19, 2022 · 4 comments
Projects

Comments

@romanzh1
Copy link

romanzh1 commented Aug 19, 2022

Hi👋.

Description

I have a small program that adds a comment to the file every run. On the second run, the file is corrupted. The bug works for the Save and AsSave functions. The comments may be different, but the problem is to re-add the comment to the same cell.

In addition, I noticed that adding a non-existing styleID to SetCellStyle func leads to file corruption.

Program

package main
import (
	"fmt"

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

func main() {
	excel, _ := excelize.OpenFile("test.xlsx")

	if err := excel.AddComment("project", "A2", fmt.Sprintf(`{"author":"system: ","text":"Test: test"}`)); err != nil {
		fmt.Printf("comment: %w\n", err)
	}

	if err := excel.Save(); err != nil {
		fmt.Printf("\ncan't save error file: %w", err)
	}
}

Result

After opening the re-saved file an error occurs that the file is corrupted

Excelize version or commit ID

I used the version of the last commit on the master branch:
commit d1e76fc
import:
github.com/xuri/excelize/v2 v2.6.1-0.20220817025952-d1e76fc432ac

Excel file

https://www.dropbox.com/scl/fi/lvqws5itronua15tpridk/test.xlsx?dl=0&rlkey=b2053854zq6qwdnt1p8d5v3qb

@xuri
Copy link
Member

xuri commented Aug 20, 2022

Thanks for your issue. There is only one comment allowed in a cell in the spreadsheet, so if you add the comment on the second run, the file will be corrupt. I've just merged pull request #1317, which introduced the new function DeleteComment to support deleting comments, please upgrade to the master branch code, and use it before adding a comment, and this feature will be released in the next version.

@romanzh1
Copy link
Author

Thanks a lot. The release is very timely)
What about SetCellStyle? Adding a non-existing styleID also results in file corruption.

@xuri
Copy link
Member

xuri commented Aug 22, 2022

I suggest that the developer make sure to use the style ID which gets from the NewStyle or GetCellStyle function, and I will consider adding a checking for the style ID.

@xuri xuri added the in progress Working in progress label Aug 31, 2022
@xuri xuri closed this as completed in 75ce231 Aug 31, 2022
@xuri xuri added this to Feature in v2.7.0 Aug 31, 2022
@xuri xuri removed the in progress Working in progress label Aug 31, 2022
@xuri
Copy link
Member

xuri commented Sep 1, 2022

I have added checking for the style ID, an error will be returned when setting the not existing style ID. Please upgrade to the master branch code, and this feature will be released in the next version.

xuri added a commit to carbin-gun/excelize that referenced this issue Oct 9, 2022
xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
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.7.0
Feature
Development

No branches or pull requests

2 participants