Skip to content
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

Ensure that only the visible columns are exported. #605

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/dash-table/components/Export/index.tsx
@@ -1,6 +1,6 @@
import XLSX from 'xlsx';
import React from 'react';
import { IDerivedData, Columns, ExportHeaders, ExportFormat, ExportColumns } from 'dash-table/components/Table/props';
import { IDerivedData, Columns, Data, Datum, ExportHeaders, ExportFormat, ExportColumns } from 'dash-table/components/Table/props';
import { createWorkbook, createHeadings, createWorksheet } from './utils';
import getHeaderRows from 'dash-table/derived/header/headerRows';

Expand All @@ -14,6 +14,21 @@ interface IExportButtonProps {
merge_duplicate_headers: boolean;
}

export function filterData(data: Data, columnID: string[]) {

let filteredData: Data = [];

data.forEach(data_row => {
let filtered_row: Datum = {};
columnID.forEach(column => {
filtered_row[column] = data_row[column];
});
filteredData.push(filtered_row);
});

return filteredData;
}

export default React.memo((props: IExportButtonProps) => {

const { columns, export_columns, export_format, virtual_data, export_headers, visibleColumns, merge_duplicate_headers } = props;
Expand All @@ -27,12 +42,12 @@ export default React.memo((props: IExportButtonProps) => {

const maxLength = getHeaderRows(columns);
const heading = (export_headers !== ExportHeaders.None) ? createHeadings(columnHeaders, maxLength) : [];
const ws = createWorksheet(heading, virtual_data.data, columnID, export_headers, merge_duplicate_headers);
const ws = createWorksheet(heading, filterData(virtual_data.data, columnID), columnID, export_headers, merge_duplicate_headers);
const wb = createWorkbook(ws);
if (export_format === ExportFormat.Xlsx) {
XLSX.writeFile(wb, 'Data.xlsx', {bookType: 'xlsx', type: 'buffer'});
XLSX.writeFile(wb, 'Data.xlsx', { bookType: 'xlsx', type: 'buffer' });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These diffs come from the linter -- I can remove them if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this PR is redundant with what's been done here #592. See https://github.com/plotly/dash-table/blame/dev/src/dash-table/components/Export/utils.tsx#L59 for column filtering.

} else if (export_format === ExportFormat.Csv) {
XLSX.writeFile(wb, 'Data.csv', {bookType: 'csv', type: 'buffer'});
XLSX.writeFile(wb, 'Data.csv', { bookType: 'csv', type: 'buffer' });
}
};

Expand Down