Skip to content

TypeError in downloadAsPivotExcel when table element is missing #39144

@bouhalimedaziz

Description

@bouhalimedaziz

Bug description

Description

While I was going through the frontend utility functions, I spotted a small but potentially disruptive bug in superset-frontend/src/utils/downloadAsPivotExcel.ts.

Currently, the exportPivotExcel function calls document.querySelector and passes the result directly into a library function without checking if the element actually exists. If there's a race condition or the selector doesn't match for any reason, querySelector returns null, which causes a TypeError.

The Bug:
In superset-frontend/src/utils/downloadAsPivotExcel.ts (lines 25–26):

const table = document.querySelector(tableSelector);
const workbook = utils.table_to_book(table); // This crashes if table is null
writeFile(workbook, `${fileName}.xlsx`);

Passing null to xlsx triggers an unhandled exception. Depending on where this is called in the UI, it can lead to a silent failure (the user clicks and nothing happens) or a full React crash if it’s not caught by an error boundary.

Proposed Fix

I’ve added a simple guard clause to bail out and log an error if the table isn't found. This prevents the crash and makes it much easier to debug why an export might be failing.

const table = document.querySelector(tableSelector);
if (!table) {
  console.error(`[exportPivotExcel] No element found for selector: "${tableSelector}"`);
  return;
}
const workbook = utils.table_to_book(table);
writeFile(workbook, `${fileName}.xlsx`);

I have a PR ready regarding this fix, and would be happy to modify it if the issue gets acknowledged but the error management doesn't fit the typical way Superset handles this kind of situation.

Screenshots/recordings

I verified the issue by mocking xlxs internal behavior in the browser console.

Image

and verified the fix by implementing the logic i shown earlier

Image

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

No response

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions