Skip to content

This closes #324, update worksheet name in chart cell reference formulas on sheet rename - #2380

Open
ChrisJr404 wants to merge 1 commit into
qax-os:masterfrom
ChrisJr404:fix-chart-ref-sheet-rename
Open

This closes #324, update worksheet name in chart cell reference formulas on sheet rename#2380
ChrisJr404 wants to merge 1 commit into
qax-os:masterfrom
ChrisJr404:fix-chart-ref-sheet-rename

Conversation

@ChrisJr404

Copy link
Copy Markdown

Description

SetSheetName renames a worksheet but does not update the cell reference formulas that charts store in xl/charts/chart*.xml. Because those formulas keep pointing at the old worksheet name (for example Sheet1!$B$2:$B$3), the reference becomes dangling once the sheet no longer exists, and Excel opens the workbook with an empty chart — the series data is lost.

This change updates the worksheet name in every chart series reference (name, categories and values) when a sheet is renamed, so charts keep their data. References to other worksheets are left untouched, and the worksheet name is quoted when it contains a space or other non-alphabetical characters, matching how Excel writes the formula.

While wiring this up I also fixed adjustRangeSheetName, which is used for renaming references in defined names. It escaped the source name before comparing, so a source worksheet whose name required quoting (e.g. My Data) never matched and was silently left unchanged. It now unescapes each reference token before comparing, so quoted source names are handled correctly for both defined names and charts. A small unescapeSheetName helper (the inverse of the existing escapeSheetName) was added for this.

Related Issue

Closes #324

Motivation and Context

Renaming a sheet that has a chart on it is a common operation, and today it silently corrupts the chart. On the issue, the maintainer noted that solving this requires checking and updating the references (such as xl/charts/chart*.xml) when a sheet is renamed; this implements that for charts.

How Has This Been Tested

  • Added TestSetSheetNameWithChart, which builds a chart with series that reference two worksheets, renames one of them to a name containing a space, and asserts that the renamed references are updated and correctly quoted, that references to the other worksheet are preserved, and that the result survives a save/reopen round trip.
  • Added TestAdjustRangeSheetName and TestUnescapeSheetName covering unquoted names, quoted (spaced) source and target names, escaped single quotes, and references to unrelated worksheets.
  • go test ./, go vet ./ and gofmt all pass; the new and changed functions are covered 100%.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

@xuri xuri added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When rename the sheet it removes the chart data from sheet.

2 participants