From aac06e50c52becf585ee82759cab052936f09612 Mon Sep 17 00:00:00 2001 From: Antony Courtney Date: Fri, 6 Oct 2023 11:30:32 -0700 Subject: [PATCH] Tweak clipboard export format to use TSV to be friendlier to Excel and Google Sheets --- packages/tadviewer/package.json | 1 - .../tadviewer/src/components/GridPane.tsx | 27 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/tadviewer/package.json b/packages/tadviewer/package.json index 7e1bff3..fb23310 100644 --- a/packages/tadviewer/package.json +++ b/packages/tadviewer/package.json @@ -28,7 +28,6 @@ "@blueprintjs/core": "4.12.0", "@blueprintjs/popover2": "1.10.1", "@types/he": "^1.1.2", - "export-to-csv": "^0.2.1", "he": "^1.2.0", "immutable": "^4.0.0", "lodash": "^4.17.21", diff --git a/packages/tadviewer/src/components/GridPane.tsx b/packages/tadviewer/src/components/GridPane.tsx index af2be74..30d2ef4 100644 --- a/packages/tadviewer/src/components/GridPane.tsx +++ b/packages/tadviewer/src/components/GridPane.tsx @@ -12,7 +12,6 @@ import { SimpleClipboard } from "./SimpleClipboard"; import { PagedDataView } from "../PagedDataView"; import { ViewParams } from "../ViewParams"; import * as util from "../util"; -import { ExportToCsv } from "export-to-csv"; import * as he from "he"; import { AppState } from "../AppState"; import { ViewState } from "../ViewState"; @@ -349,6 +348,14 @@ const getGridOptionsFromStateRef = (stateRef: StateRef) => { return getGridOptions(appState.showColumnHistograms, appState.viewState); }; +// escape tabs by placing string in quotes +function escapeTabs(cellData: any): any { + if (typeof cellData === "string" && cellData.indexOf("\t") >= 0) { + return '"' + cellData.replace(/"/g, '""') + '"'; + } + return cellData; +} + /* Create grid from the specified set of columns */ const createGrid = ( stateRef: StateRef, @@ -375,7 +382,7 @@ const createGrid = ( grid.registerPlugin(new AutoTooltips({ enableForCells: true })); const copySelectedRange = async (range: any) => { - let copyData = []; + let copyRowStrings: string[] = []; const gridCols = grid.getColumns(); const gridData = grid.getData(); for (let row = range.fromRow; row <= range.toRow; row++) { @@ -383,20 +390,12 @@ const createGrid = ( const copyRow = []; for (let col = range.fromCell; col <= range.toCell; col++) { const cid = gridCols[col].id; - copyRow.push(rowData[cid]); + copyRow.push(escapeTabs(rowData[cid])); } - copyData.push(copyRow); - } - try { - // const data = await csv.writeToString(copyData, { headers: false }); - const csvExporter = new ExportToCsv(); - const data = csvExporter.generateCsv(copyData, true); - console.log("writing text to clipboard: ", data); - clipboard.writeText(data); - } catch (err) { - console.error("error converting copied data to CSV: ", err); - return; + copyRowStrings.push(copyRow.join("\t")); } + const copyData = copyRowStrings.join("\r\n") + "\r\n"; + clipboard.writeText(copyData); }; copyManager.onCopyCells.subscribe(async (e: any, args: any) => {