Skip to content
Merged
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
10 changes: 6 additions & 4 deletions src/chart/ChartUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,23 @@ export function getRecordType(value) {
/**
* Basic function to convert a table row output to a CSV file, and download it.
* TODO: Make this more robust. Probably the commas should be escaped to ensure the CSV is always valid.
* seperator should be either ',', ';' or 'tab'
*/
export const downloadCSV = (rows) => {
export const downloadCSV = (rows, seperator) => {
const sep = seperator == 'tab' ? '\t' : seperator;
const element = document.createElement('a');
let csv = '';
const headers = Object.keys(rows[0]).slice(1);
csv += `${headers.join(', ')}\n`;
csv += `${headers.join(sep)}\n`;
rows.forEach((row) => {
headers.forEach((header) => {
// Parse value
let value = row[header];
if (value && value.low) {
value = value.low;
}
csv += JSON.stringify(value).replaceAll(',', ';');
csv += headers.indexOf(header) < headers.length - 1 ? ', ' : '';
csv += JSON.stringify(value).replaceAll(sep, sep == ',' ? ';' : ',');
csv += headers.indexOf(header) < headers.length - 1 ? sep : '';
});
csv += '\n';
});
Expand Down
3 changes: 2 additions & 1 deletion src/chart/table/TableChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const NeoTableChart = (props: ChartProps) => {
const allowDownload =
props.settings && props.settings.allowDownload !== undefined ? props.settings.allowDownload : false;
const compact = props.settings && props.settings.compact !== undefined ? props.settings.compact : false;
const separator = props.settings && props.settings.separator !== undefined ? props.settings.separator : ',';
const styleRules = useStyleRules(
extensionEnabled(props.extensions, 'styling'),
props.settings.styleRules,
Expand Down Expand Up @@ -146,7 +147,7 @@ const NeoTableChart = (props: ChartProps) => {
<Tooltip title='Download CSV' aria-label=''>
<IconButton
onClick={() => {
downloadCSV(rows);
downloadCSV(rows, separator);
}}
aria-label='download csv'
style={{ bottom: '9px', left: '3px', position: 'absolute' }}
Expand Down
6 changes: 6 additions & 0 deletions src/config/ReportConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export const REPORT_TYPES = {
values: [true, false],
default: false,
},
separator: {
label: 'CSV Field Seperator',
type: SELECTION_TYPES.LIST,
values: [',', ';', 'tab'],
default: ',',
},
refreshButtonEnabled: {
label: 'Refreshable',
type: SELECTION_TYPES.LIST,
Expand Down