-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Lens][Visualize][Inspector][Reporting] Unified check for CSV cells for known formula characters (and value escaping more in general) #105221
Changes from 4 commits
fd94375
2601e6a
1f921ce
a6feca1
53bc2b8
14f2401
b8d2add
d65d858
756e252
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const CSV_FORMULA_CHARS = ['=', '+', '-', '@']; | ||
export const nonAlphaNumRE = /[^a-zA-Z0-9]/; | ||
export const allDoubleQuoteRE = /"/g; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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 { startsWith } from 'lodash'; | ||
import { Datatable } from 'src/plugins/expressions'; | ||
import { CSV_FORMULA_CHARS } from './constants'; | ||
|
||
export const cellHasFormulas = (val: string) => | ||
CSV_FORMULA_CHARS.some((formulaChar) => startsWith(val, formulaChar)); | ||
|
||
export const tableHasFormulas = (columns: Datatable['columns'], rows: Datatable['rows']) => { | ||
return ( | ||
columns.some(({ name }) => cellHasFormulas(name)) || | ||
rows.some((row) => columns.some(({ id }) => cellHasFormulas(row[id]))) | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,14 @@ import PropTypes from 'prop-types'; | |
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { EuiButton, EuiContextMenuItem, EuiContextMenuPanel, EuiPopover } from '@elastic/eui'; | ||
import { CSV_MIME_TYPE, datatableToCSV } from '../../../../common'; | ||
import { | ||
EuiButton, | ||
EuiContextMenuItem, | ||
EuiContextMenuPanel, | ||
EuiPopover, | ||
EuiToolTip, | ||
} from '@elastic/eui'; | ||
import { CSV_MIME_TYPE, datatableToCSV, tableHasFormulas } from '../../../../common'; | ||
import { Datatable } from '../../../../../expressions'; | ||
import { downloadMultipleAs } from '../../../../../share/public'; | ||
import { FieldFormatsStart } from '../../../field_formats'; | ||
|
@@ -96,6 +102,9 @@ class DataDownloadOptions extends Component<DataDownloadOptionsProps, DataDownlo | |
}; | ||
|
||
renderFormattedDownloads() { | ||
const detectedFormulasInTables = this.props.datatables.some(({ columns, rows }) => | ||
tableHasFormulas(columns, rows) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful to memoize the result of this check and only re-run it when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did add the memoization on this specific instance. Or perhaps you meant something more generic like on the |
||
const button = ( | ||
<EuiButton iconType="arrowDown" iconSide="right" size="s" onClick={this.onTogglePopover}> | ||
<FormattedMessage | ||
|
@@ -104,6 +113,20 @@ class DataDownloadOptions extends Component<DataDownloadOptionsProps, DataDownlo | |
/> | ||
</EuiButton> | ||
); | ||
const downloadButton = detectedFormulasInTables ? ( | ||
<EuiToolTip | ||
position="top" | ||
content={i18n.translate('data.inspector.table.exportButtonFormulasWarning', { | ||
defaultMessage: | ||
'Your CSV contains characters which spreadsheet application can interpret as formulas', | ||
})} | ||
> | ||
{button} | ||
</EuiToolTip> | ||
) : ( | ||
button | ||
); | ||
|
||
const items = [ | ||
<EuiContextMenuItem | ||
key="csv" | ||
|
@@ -139,7 +162,7 @@ class DataDownloadOptions extends Component<DataDownloadOptionsProps, DataDownlo | |
return ( | ||
<EuiPopover | ||
id="inspectorDownloadData" | ||
button={button} | ||
button={downloadButton} | ||
isOpen={this.state.isPopoverOpen} | ||
closePopover={this.closePopover} | ||
panelPaddingSize="none" | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,7 +7,13 @@ | |||||
*/ | ||||||
|
||||||
import React, { memo, useState, useCallback } from 'react'; | ||||||
import { EuiButtonEmpty, EuiContextMenuItem, EuiContextMenuPanel, EuiPopover } from '@elastic/eui'; | ||||||
import { | ||||||
EuiButtonEmpty, | ||||||
EuiContextMenuItem, | ||||||
EuiContextMenuPanel, | ||||||
EuiPopover, | ||||||
EuiToolTip, | ||||||
} from '@elastic/eui'; | ||||||
import { FormattedMessage } from '@kbn/i18n/react'; | ||||||
import { i18n } from '@kbn/i18n'; | ||||||
|
||||||
|
@@ -39,6 +45,8 @@ export const TableVisControls = memo( | |||||
services: { uiSettings }, | ||||||
} = useKibana<CoreStart>(); | ||||||
|
||||||
const detectedFormulasInTable = exporters.tableHasFormulas(columns, rows); | ||||||
|
||||||
const onClickExport = useCallback( | ||||||
(formatted: boolean) => { | ||||||
const csvSeparator = uiSettings.get(CSV_SEPARATOR_SETTING); | ||||||
|
@@ -85,6 +93,20 @@ export const TableVisControls = memo( | |||||
</EuiButtonEmpty> | ||||||
); | ||||||
|
||||||
const downloadButton = detectedFormulasInTable ? ( | ||||||
<EuiToolTip | ||||||
position="top" | ||||||
content={i18n.translate('visTypeTable.vis.controls.exportButtonFormulasWarning', { | ||||||
defaultMessage: | ||||||
'Your CSV contains characters which spreadsheet application can interpret as formulas', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the same typo in all the text you've added:
Suggested change
|
||||||
})} | ||||||
> | ||||||
{button} | ||||||
</EuiToolTip> | ||||||
) : ( | ||||||
button | ||||||
); | ||||||
|
||||||
const items = [ | ||||||
<EuiContextMenuItem key="rawCsv" onClick={() => onClickExport(false)}> | ||||||
<FormattedMessage id="visTypeTable.vis.controls.rawCSVButtonLabel" defaultMessage="Raw" /> | ||||||
|
@@ -100,7 +122,7 @@ export const TableVisControls = memo( | |||||
return ( | ||||||
<EuiPopover | ||||||
id="dataTableExportData" | ||||||
button={button} | ||||||
button={downloadButton} | ||||||
isOpen={isPopoverOpen} | ||||||
closePopover={closePopover} | ||||||
panelPaddingSize="none" | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a required argument instead? I found it hard to trace the code that is using the default value here vs actually providing a value. It looks like only the Reporting integration has a way to escape formulas, with the
xpack.reporting.csv.escapeFormulaValues
setting, and I was expecting that other places in the code would also use escaping.