fix(excel): sanitize_sheet_name leaves trailing apostrophe when truncated at 31 chars, crashing dashboard XLSX export
Bug description
When exporting a dashboard to Excel (multi-sheet streaming XLSX export), StreamingXlsxWriter uses sanitize_sheet_name(raw, used) in superset/utils/excel_streaming.py to sanitize chart and tab names for Excel worksheets.
Excel worksheet naming rules forbid sheet names starting or ending with an apostrophe ('). XlsxWriter strictly enforces this constraint:
xlsxwriter.exceptions.InvalidWorksheetName: Sheet name cannot start or end with an apostrophe "..."
In sanitize_sheet_name:
name = _INVALID_SHEET_CHARS_RE.sub("_", raw or "")
name = name.strip().strip("'").strip()
if not name:
name = "Sheet"
if name.lower() == _RESERVED_SHEET_NAME:
name = f"{name}_"
name = name[:MAX_SHEET_NAME_LEN]
Apostrophes are stripped before the string is sliced to MAX_SHEET_NAME_LEN (31 chars). If a chart or dashboard slice title contains an apostrophe that ends up at the 31-character truncation boundary (e.g. raw = "Quarterly Revenue Summary: Men's Clothing"), name[:31] truncates right after the apostrophe, yielding:
"Quarterly Revenue Summary_ Men'"
Because trailing apostrophes and whitespace are not stripped after slicing, the sanitized sheet name ends with '.
When StreamingXlsxWriter.add_sheet() or StreamingXlsxWriter.add_image_sheet() subsequently calls self._workbook.add_worksheet(sheet_name), xlsxwriter raises an unhandled InvalidWorksheetName exception, aborting the entire dashboard XLSX export with a 500 error.
How to reproduce the bug
- Create a dashboard with a chart named
"Quarterly Revenue Summary: Men's Clothing". - Export the dashboard to Excel (XLSX).
- The export fails with:
xlsxwriter.exceptions.InvalidWorksheetName: Sheet name cannot start or end with an apostrophe "Quarterly Revenue Summary_ Men'"
Expected behavior
sanitize_sheet_name should ensure the returned sheet name never starts or ends with an apostrophe or whitespace, even after length truncation and deduplication suffixing. Truncating at 31 characters should strip trailing apostrophes (e.g. yielding "Quarterly Revenue Summary_ Men").