Skip to content

DefinedNames SheetNames are not escaped correctly, if a sheet gets renamed #2127

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

Merged
merged 9 commits into from
May 8, 2025

Conversation

R3dByt3
Copy link
Contributor

@R3dByt3 R3dByt3 commented May 7, 2025

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

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link

codecov bot commented May 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.23%. Comparing base (8fdc26e) to head (43c6572).
Report is 1 commits behind head on master.

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              
Flag Coverage Δ
unittests 99.23% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR. I've left a comment.

@xuri xuri added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 8, 2025
@xuri xuri moved this to Bugfix in Excelize v2.9.1 May 8, 2025
@xuri xuri linked an issue May 8, 2025 that may be closed by this pull request
2 tasks
@R3dByt3 R3dByt3 requested a review from xuri May 8, 2025 07:54
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, ",")
}

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented May 8, 2025

@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 👍)!

@R3dByt3 R3dByt3 requested a review from xuri May 8, 2025 10:58
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks for your contribution.

@xuri xuri added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 8, 2025
@xuri xuri merged commit d230ecd into qax-os:master May 8, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
No open projects
Status: Bugfix
Development

Successfully merging this pull request may close these issues.

DefinedNames SheetNames are not escaped correctly, if a sheet gets renamed
2 participants