Skip to content

Commit

Permalink
added setting for CR/LF handling
Browse files Browse the repository at this point in the history
  • Loading branch information
metawops committed Jun 11, 2022
1 parent 23be371 commit 574d760
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
51 changes: 40 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Table2CSVSettings {
sepChar: string;
quoteData: boolean;
saveToClipboardToo: boolean;
removeCRLF: string;
}

const DEFAULT_SETTINGS: Table2CSVSettings = {
Expand All @@ -26,7 +27,8 @@ const DEFAULT_SETTINGS: Table2CSVSettings = {
fileNumber: '001',
sepChar: ',',
quoteData: false,
saveToClipboardToo: false
saveToClipboardToo: false,
removeCRLF: 'removeCRLF-space'
}

export default class Table2CSVPlugin extends Plugin {
Expand All @@ -49,7 +51,7 @@ export default class Table2CSVPlugin extends Plugin {
const viewMode = view.getMode();
if (viewMode=="preview") {
// Now convert the tables
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData);
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData, this.settings.removeCRLF);

// TODO: prüfen, ob csvString leer oder nicht! Nur wenn nicht, Datei anlegen etc.
if (csvString.length > 0) {
Expand Down Expand Up @@ -108,6 +110,8 @@ export default class Table2CSVPlugin extends Plugin {

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new Table2CSVSettingTab(this.app, this));

console.log(`Table to CSV plugin: Version ${this.manifest.version} loaded.`);
}

onunload() {
Expand All @@ -123,29 +127,40 @@ export default class Table2CSVPlugin extends Plugin {
}


function htmlToCSV(html: HTMLElement, sep: string, quote: boolean) {
function htmlToCSV(html: HTMLElement, sep: string, quote: boolean, removeCRLF: string) {
var data = [];
var table = html.querySelector("table");
console.log(`htmlToCSV::table: ${table}`);
//console.log(`htmlToCSV::table: ${table}`);

if (table) {
var rows = table.rows;
console.log(`htmlToCSV::rows: ${rows}`);
//console.log(`htmlToCSV::rows: ${rows}`);
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");

for (var j = 0; j < cols.length; j++) {
if (!quote) {
row.push((cols[j] as HTMLElement).innerText);
} else {
row.push('"' + (cols[j] as HTMLElement).innerText + '"');
var cellContent = (cols[j] as HTMLElement).innerText;

// handle the optional replacement of CR/LF characters:
if (removeCRLF=='removeCRLF-clear') {
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, "");
} else if (removeCRLF=='removeCRLF-space') {
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, " ");
} else if (removeCRLF=='removeCRLF-string1') {
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, "[CR]");
}

// handle the quoting of data cells:
// for now it's just the hard-coded character "
if (quote) cellContent = '"' + cellContent + '"';

row.push(cellContent);
}

data.push(row.join(sep));
}
}
console.log(`htmlToCSV::data.length: ${data.length}`);
//console.log(`htmlToCSV::data.length: ${data.length}`);
if (data.length > 0)
return data.join("\n");
else
Expand Down Expand Up @@ -220,7 +235,7 @@ class Table2CSVSettingTab extends PluginSettingTab {

new Setting(containerEl)
.setName('Quote data')
.setDesc('Do you want quotation marks around each cell\'s data?')
.setDesc('Do you want quotation marks (") around each cell\'s data?')
.addToggle( toggle => toggle
.setValue(this.plugin.settings.quoteData)
.onChange(async (value) => {
Expand All @@ -239,5 +254,19 @@ class Table2CSVSettingTab extends PluginSettingTab {
this.plugin.settings.saveToClipboardToo = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Handling of CR/LF in data')
.setDesc('Chose how to handle the occurance of return and linefeed characters in data cells.')
.addDropdown( dropdown => dropdown
.addOption('removeCRLF-clear', 'Remove all CR & LF characters')
.addOption('removeCRLF-space', 'Replace all CR & LF characters with one space')
.addOption('removeCRLF-string1', 'Replace all CR & LF characters with string [CR]')
.setValue(this.plugin.settings.removeCRLF)
.onChange(async (value) => {
this.plugin.settings.removeCRLF = value;
await this.plugin.saveSettings();
}))

}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-table-to-csv-exporter",
"name": "Table to CSV Exporter",
"version": "0.1.2",
"version": "0.1.3",
"minAppVersion": "0.14.6",
"description": "This plugin allows for exporting tables from a pane in reading mode into CSV files.",
"author": "Stefan Wolfrum",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-table-to-csv-exporter",
"version": "0.1.2",
"version": "0.1.3",
"description": "This plugin allows to export tables in a preview pane to be exported to CSV files.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 574d760

Please sign in to comment.