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

[Datatable][Inspector] Make inspectable/exportable only visible columns #122268

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/plugins/data/common/exports/export_csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,28 @@ describe('CSV exporter', () => {
})
).toMatch('columnOne\r\n"\'=1"\r\n');
});

test('should filter out columns when there are meta information', () => {
const datatable = getDataTable({ multipleColumns: true });
// Make only the first column visible
datatable.columns[0].meta.dimensionName = 'First column';
expect(
datatableToCSV(datatable, {
...getDefaultOptions(),
escapeFormulaValues: true,
formatFactory: () => ({ convert: (v: unknown) => v } as FieldFormat),
})
).toMatch('columnOne\r\nvalue\r\n');
});

test('should not filter out columns if no meta information are found', () => {
const datatable = getDataTable({ multipleColumns: true });
expect(
datatableToCSV(datatable, {
...getDefaultOptions(),
escapeFormulaValues: true,
formatFactory: () => ({ convert: (v: unknown) => v } as FieldFormat),
})
).toMatch('columnOne,columnTwo\r\nvalue,5\r\n');
});
});
11 changes: 8 additions & 3 deletions src/plugins/data/common/exports/export_csv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { Datatable } from 'src/plugins/expressions';
import { FormatFactory } from '../../../field_formats/common';
import { createEscapeValue } from './escape_value';
import { getVisibleColumns } from './table_filter';

export const LINE_FEED_CHARACTER = '\r\n';
export const CSV_MIME_TYPE = 'text/plain;charset=utf-8';
Expand All @@ -28,10 +29,14 @@ export function datatableToCSV(
{ csvSeparator, quoteValues, formatFactory, raw, escapeFormulaValues }: CSVOptions
) {
const escapeValues = createEscapeValue(quoteValues, escapeFormulaValues);

// If columns have some meta information, use them to filter out hidden columns
const filteredColumns = getVisibleColumns(columns);
// const filteredColumns = columns;
// Build the header row by its names
const header = columns.map((col) => escapeValues(col.name));
const header = filteredColumns.map((col) => escapeValues(col.name));

const formatters = columns.reduce<Record<string, ReturnType<FormatFactory>>>(
const formatters = filteredColumns.reduce<Record<string, ReturnType<FormatFactory>>>(
(memo, { id, meta }) => {
memo[id] = formatFactory(meta?.params);
return memo;
Expand All @@ -41,7 +46,7 @@ export function datatableToCSV(

// Convert the array of row objects to an array of row arrays
const csvRows = rows.map((row) => {
return columns.map((column) =>
return filteredColumns.map((column) =>
escapeValues(raw ? row[column.id] : formatters[column.id].convert(row[column.id]))
);
});
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/data/common/exports/table_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DatatableColumn } from 'src/plugins/expressions';

export function getVisibleColumns(columns: DatatableColumn[]) {
const columnsHaveMetaInfo = columns.some(({ meta }) => meta?.dimensionName);
return columnsHaveMetaInfo ? columns.filter(({ meta }) => meta?.dimensionName) : columns;
}
Loading