DefinedNames SheetNames are not escaped correctly, if a sheet gets renamed#2127
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2127 +/- ##
==========================================
- Coverage 99.23% 99.23% -0.01%
==========================================
Files 32 32
Lines 30222 30218 -4
==========================================
- Hits 29991 29987 -4
Misses 153 153
Partials 78 78
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
xuri
left a comment
There was a problem hiding this comment.
Thanks for your PR. I've left a comment.
xuri
left a comment
There was a problem hiding this comment.
Hi @R3dByt3, I found new issue on update defined name with multiple worksheets. Please consider this case:
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
if _, err := f.NewSheet("Sheet 3"); err != nil {
fmt.Println(err)
return
}
if err := f.SetDefinedName(&excelize.DefinedName{
Name: "Amount3",
RefersTo: "'Sheet 3'!$A$2:$D$5",
Comment: "defined name comment",
Scope: "Sheet3",
}); err != nil {
fmt.Println(err)
return
}
if err := f.SetDefinedName(&excelize.DefinedName{
Name: "Amount",
RefersTo: "Sheet1!$A$2:$D$5",
Comment: "defined name comment",
Scope: "Sheet2",
}); err != nil {
fmt.Println(err)
return
}
if err := f.SetSheetName("Sheet1", "Requires escaping"); err != nil {
fmt.Println(err)
return
}
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}The defined name "Amount3" shouldn't be changed.
- Expected:
'Sheet 3'!$A$2:$D$5 - Actual:
Sheet '3'!$A$2:$D$5
The fix could be like this:
func adjustRangeSheetName(rng, source, target string) string {
source = escapeSheetName(source)
cellRefs := strings.Split(rng, ",")
for i, cellRef := range cellRefs {
rangeRefs := strings.Split(cellRef, ":")
for j, rangeRef := range rangeRefs {
parts := strings.Split(rangeRef, "!")
for k, part := range parts {
if strings.TrimPrefix(strings.TrimSuffix(part, "'"), "'") == source {
part = escapeSheetName(target)
}
parts[k] = part
}
rangeRefs[j] = strings.Join(parts, "!")
}
cellRefs[i] = strings.Join(rangeRefs, ":")
}
return strings.Join(cellRefs, ",")
}|
@xuri I think you are right and your proposed solution fixes this issue once and for all, thank you very much - good catch again (You are a pretty good reviewer 👍)! |
xuri
left a comment
There was a problem hiding this comment.
Great work! Thanks for your contribution.
PR Details
DefinedNames SheetNames are not escaped correctly, if a sheet gets renamed
Description
Renaming a sheet might cause Microsoft Excel to show a warning that the file contains broken links. This happens if the sheet name contains whitespaces for example and definedNames referencing this sheet. This is also the reason why some sheet names are escaped and others not. This PR checks via regex for non word characters and escapes sheet names in definedNames if required. The current implementation only preserves already present escaping, even if it is not required anymore.
Currently "Sheet 2!$A$1:$A$2"
Required "'Sheet 2'!$A$1:$A$2"
Related Issue
#2126
Motivation and Context
Fixes "broken links".
How Has This Been Tested
Tested in a local project.
Types of changes
Checklist