Skip to content

Commit

Permalink
Tweak clipboard export format to use TSV to be friendlier to Excel an…
Browse files Browse the repository at this point in the history
…d Google Sheets
  • Loading branch information
antonycourtney committed Oct 6, 2023
1 parent b968879 commit aac06e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/tadviewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 13 additions & 14 deletions packages/tadviewer/src/components/GridPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -349,6 +348,14 @@ const getGridOptionsFromStateRef = (stateRef: StateRef<AppState>) => {
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<AppState>,
Expand All @@ -375,28 +382,20 @@ 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++) {
const rowData = gridData.getItem(row);
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) => {
Expand Down

0 comments on commit aac06e5

Please sign in to comment.