Skip to content

Commit 48255d3

Browse files
committed
dropdowns in setting for quote & sep char
1 parent 574d760 commit 48255d3

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

main.ts

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Table2CSVSettings {
1616
baseFilename: string;
1717
fileNumber: string;
1818
sepChar: string;
19-
quoteData: boolean;
19+
quoteDataChar: string;
2020
saveToClipboardToo: boolean;
2121
removeCRLF: string;
2222
}
@@ -25,8 +25,8 @@ const DEFAULT_SETTINGS: Table2CSVSettings = {
2525
exportPath: './',
2626
baseFilename: 'table-export',
2727
fileNumber: '001',
28-
sepChar: ',',
29-
quoteData: false,
28+
sepChar: 'sepChar-semicolon',
29+
quoteDataChar: 'quoteChar-doubleQuotes',
3030
saveToClipboardToo: false,
3131
removeCRLF: 'removeCRLF-space'
3232
}
@@ -51,9 +51,9 @@ export default class Table2CSVPlugin extends Plugin {
5151
const viewMode = view.getMode();
5252
if (viewMode=="preview") {
5353
// Now convert the tables
54-
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData, this.settings.removeCRLF);
55-
56-
// TODO: prüfen, ob csvString leer oder nicht! Nur wenn nicht, Datei anlegen etc.
54+
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteDataChar, this.settings.removeCRLF);
55+
56+
// If csvString is not empty, create file:
5757
if (csvString.length > 0) {
5858
const filename = `${this.settings.baseFilename}-${this.settings.fileNumber}.csv`;
5959
this.app.vault.create(filename, csvString)
@@ -127,7 +127,7 @@ export default class Table2CSVPlugin extends Plugin {
127127
}
128128

129129

130-
function htmlToCSV(html: HTMLElement, sep: string, quote: boolean, removeCRLF: string) {
130+
function htmlToCSV(html: HTMLElement, sepMode: string, quoteChar: string, removeCRLF: string) {
131131
var data = [];
132132
var table = html.querySelector("table");
133133
//console.log(`htmlToCSV::table: ${table}`);
@@ -152,12 +152,39 @@ function htmlToCSV(html: HTMLElement, sep: string, quote: boolean, removeCRLF: s
152152

153153
// handle the quoting of data cells:
154154
// for now it's just the hard-coded character "
155-
if (quote) cellContent = '"' + cellContent + '"';
156-
155+
if (quoteChar=='quoteChar-doubleQuotes') {
156+
cellContent = '"' + cellContent + '"';
157+
} else if (quoteChar=='quoteChar-singleQuotes') {
158+
cellContent = "'" + cellContent + "'";
159+
}
157160
row.push(cellContent);
158161
}
159-
160-
data.push(row.join(sep));
162+
163+
var sepChar = ';';
164+
switch(sepMode) {
165+
case 'sepChar-semicolon':
166+
sepChar = ';';
167+
break;
168+
case 'sepChar-comma':
169+
sepChar = ',';
170+
break;
171+
case 'sepChar-tab':
172+
sepChar = '\t';
173+
break;
174+
case 'sepChar-pipe':
175+
sepChar = '|';
176+
break;
177+
case 'sepChar-tilde':
178+
sepChar = '~';
179+
break;
180+
case 'sepChar-caret':
181+
sepChar = '^';
182+
break;
183+
case 'sepChar-colon':
184+
sepChar = ':';
185+
break;
186+
}
187+
data.push(row.join(sepChar));
161188
}
162189
}
163190
//console.log(`htmlToCSV::data.length: ${data.length}`);
@@ -223,9 +250,15 @@ class Table2CSVSettingTab extends PluginSettingTab {
223250

224251
new Setting(containerEl)
225252
.setName('Data fields separation character/string')
226-
.setDesc('This character (or string) will be put between each cell\'s data. Defaults to a comma. Special characters (like \\t for a TAB) don\'t work yet.')
227-
.addText(text => text
228-
.setPlaceholder('<enter a separation character or string>')
253+
.setDesc('This character will be put between each cell\'s data. Defaults to a semicolon.')
254+
.addDropdown(dropdown => dropdown
255+
.addOption('sepChar-semicolon', '; (semicolon)')
256+
.addOption('sepChar-comma', ', (comma)')
257+
.addOption('sepChar-tab', '\\t (tab)')
258+
.addOption('sepChar-pipe', '| (pipe)')
259+
.addOption('sepChar-tilde', '~ (tilde)')
260+
.addOption('sepChar-caret', '^ (caret)')
261+
.addOption('sepChar-colon', ': (colon)')
229262
.setValue(this.plugin.settings.sepChar)
230263
.onChange(async (value) => {
231264
//console.log('sepChar: ' + value);
@@ -235,26 +268,18 @@ class Table2CSVSettingTab extends PluginSettingTab {
235268

236269
new Setting(containerEl)
237270
.setName('Quote data')
238-
.setDesc('Do you want quotation marks (") around each cell\'s data?')
239-
.addToggle( toggle => toggle
240-
.setValue(this.plugin.settings.quoteData)
271+
.setDesc('Do you want quotation marks around each cell\'s data?')
272+
.addDropdown( dropdown => dropdown
273+
.addOption('quoteChar-noQuote', 'Don\'t quote data')
274+
.addOption('quoteChar-doubleQuotes', 'Quote data with double quote character (")')
275+
.addOption('quoteChar-singleQuotes', 'Quote data with single quote character (\')')
276+
.setValue(this.plugin.settings.quoteDataChar)
241277
.onChange(async (value) => {
242278
//console.log('quote data toggle: ' + value);
243-
this.plugin.settings.quoteData = value;
279+
this.plugin.settings.quoteDataChar = value;
244280
await this.plugin.saveSettings();
245281
}));
246282

247-
new Setting(containerEl)
248-
.setName('Copy to clipboard, too')
249-
.setDesc('Do you want to copy the contents of the CSV file to the system clipboard, too?')
250-
.addToggle( toggle => toggle
251-
.setValue(this.plugin.settings.saveToClipboardToo)
252-
.onChange(async (value) => {
253-
//console.log('save to clipboard, too: ' + value);
254-
this.plugin.settings.saveToClipboardToo = value;
255-
await this.plugin.saveSettings();
256-
}));
257-
258283
new Setting(containerEl)
259284
.setName('Handling of CR/LF in data')
260285
.setDesc('Chose how to handle the occurance of return and linefeed characters in data cells.')
@@ -267,6 +292,17 @@ class Table2CSVSettingTab extends PluginSettingTab {
267292
this.plugin.settings.removeCRLF = value;
268293
await this.plugin.saveSettings();
269294
}))
295+
296+
new Setting(containerEl)
297+
.setName('Copy to clipboard, too')
298+
.setDesc('Do you want to copy the contents of the CSV file to the system clipboard, too?')
299+
.addToggle( toggle => toggle
300+
.setValue(this.plugin.settings.saveToClipboardToo)
301+
.onChange(async (value) => {
302+
//console.log('save to clipboard, too: ' + value);
303+
this.plugin.settings.saveToClipboardToo = value;
304+
await this.plugin.saveSettings();
305+
}));
270306

271307
}
272308
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-table-to-csv-exporter",
33
"name": "Table to CSV Exporter",
4-
"version": "0.1.3",
4+
"version": "0.1.4",
55
"minAppVersion": "0.14.6",
66
"description": "This plugin allows for exporting tables from a pane in reading mode into CSV files.",
77
"author": "Stefan Wolfrum",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-table-to-csv-exporter",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "This plugin allows to export tables in a preview pane to be exported to CSV files.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)