This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 056fd218d44 fix: handle missing table element in downloadAsPivotExcel
(#39386)
056fd218d44 is described below
commit 056fd218d441e0bd0c99b49c86c1d93591b97836
Author: bouhalimedaziz <[email protected]>
AuthorDate: Tue Sep 8 18:11:03 2026 +0100
fix: handle missing table element in downloadAsPivotExcel (#39386)
---
.../src/utils/downloadAsPivotExcel.test.ts | 19 +++++++++++++++++++
superset-frontend/src/utils/downloadAsPivotExcel.ts | 7 +++++++
2 files changed, 26 insertions(+)
diff --git a/superset-frontend/src/utils/downloadAsPivotExcel.test.ts
b/superset-frontend/src/utils/downloadAsPivotExcel.test.ts
index d48b067b068..5418ec0888e 100644
--- a/superset-frontend/src/utils/downloadAsPivotExcel.test.ts
+++ b/superset-frontend/src/utils/downloadAsPivotExcel.test.ts
@@ -18,6 +18,7 @@
*/
import type { WorkBook } from 'xlsx';
import { getNumberFormatterRegistry } from '@superset-ui/core';
+import { logging } from '@apache-superset/core/utils';
import exportPivotExcel from './downloadAsPivotExcel';
const mockWriteFile = jest.fn();
@@ -30,6 +31,14 @@ jest.mock('xlsx', () => {
};
});
+jest.mock('@apache-superset/core/utils', () => ({
+ logging: { error: jest.fn() },
+}));
+
+afterEach(() => {
+ jest.restoreAllMocks();
+});
+
// Renders a single-row pivot table with the given cell values, runs the
// export, and returns the resulting sheet so each test only has to state
// its input cells and assertions.
@@ -114,3 +123,13 @@ test('leaves date-shaped strings as text rather than
reinterpreting them as date
expect(sheet.B1).toMatchObject({ t: 's', v: '2024-01-01 13:45:30' });
expect(sheet.C1).toMatchObject({ t: 's', v: 'not-a-date' });
});
+
+test('should log an error and return early when table element is not found',
() => {
+ jest.spyOn(document, 'querySelector').mockReturnValue(null);
+
+ exportPivotExcel('.non-existent-selector', 'test-file');
+
+ expect(logging.error as jest.Mock).toHaveBeenCalledWith(
+ '[exportPivotExcel] No element found for selector:
".non-existent-selector"',
+ );
+});
diff --git a/superset-frontend/src/utils/downloadAsPivotExcel.ts
b/superset-frontend/src/utils/downloadAsPivotExcel.ts
index 4bdf2b307c7..edd4d6c8b99 100644
--- a/superset-frontend/src/utils/downloadAsPivotExcel.ts
+++ b/superset-frontend/src/utils/downloadAsPivotExcel.ts
@@ -17,6 +17,7 @@
* under the License.
*/
import { getNumberFormatterRegistry } from '@superset-ui/core';
+import { logging } from '@apache-superset/core/utils';
import { utils, writeFile } from 'xlsx';
import type { WorkSheet } from 'xlsx';
@@ -65,6 +66,12 @@ export default function exportPivotExcel(
fileName: string,
) {
const table = document.querySelector(tableSelector);
+ if (!table) {
+ logging.error(
+ `[exportPivotExcel] No element found for selector: "${tableSelector}"`,
+ );
+ return;
+ }
// `raw: true` keeps every cell as the literal text rendered in the DOM.
// Without it, SheetJS tries to infer numbers/dates from the displayed
// string, which mangles values that were formatted using a non-US