diff --git a/src/plugins/data/common/exports/export_csv.tsx b/src/plugins/data/common/exports/export_csv.tsx index 68f817d1b148..de12a1c6d517 100644 --- a/src/plugins/data/common/exports/export_csv.tsx +++ b/src/plugins/data/common/exports/export_csv.tsx @@ -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'; @@ -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>>( + const formatters = filteredColumns.reduce>>( (memo, { id, meta }) => { memo[id] = formatFactory(meta?.params); return memo; @@ -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])) ); }); diff --git a/src/plugins/data/common/exports/table_filter.ts b/src/plugins/data/common/exports/table_filter.ts new file mode 100644 index 000000000000..396666b9d9a7 --- /dev/null +++ b/src/plugins/data/common/exports/table_filter.ts @@ -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; +} diff --git a/src/plugins/data/public/utils/table_inspector_view/components/data_table.tsx b/src/plugins/data/public/utils/table_inspector_view/components/data_table.tsx index 462193766447..2d06384689d6 100644 --- a/src/plugins/data/public/utils/table_inspector_view/components/data_table.tsx +++ b/src/plugins/data/public/utils/table_inspector_view/components/data_table.tsx @@ -20,6 +20,7 @@ import { import { FormattedMessage } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; +import { getVisibleColumns } from '../../../../common/exports/table_filter'; import { DataViewRow, DataViewColumn } from '../types'; import { IUiSettingsClient } from '../../../../../../core/public'; import { Datatable, DatatableColumn } from '../../../../../expressions/public'; @@ -156,7 +157,7 @@ export class DataTableFormat extends Component { + const columns = getVisibleColumns(data.columns).map((dataColumn: any, index: number) => { const formatParams = { id: 'string', ...dataColumn.meta.params }; const fieldFormatter = fieldFormats.deserialize(formatParams); const filterable = isFilterable(dataColumn);