@@ -18,6 +18,7 @@ interface Table2CSVSettings {
1818 sepChar : string ;
1919 quoteData : boolean ;
2020 saveToClipboardToo : boolean ;
21+ removeCRLF : string ;
2122}
2223
2324const DEFAULT_SETTINGS : Table2CSVSettings = {
@@ -26,7 +27,8 @@ const DEFAULT_SETTINGS: Table2CSVSettings = {
2627 fileNumber : '001' ,
2728 sepChar : ',' ,
2829 quoteData : false ,
29- saveToClipboardToo : false
30+ saveToClipboardToo : false ,
31+ removeCRLF : 'removeCRLF-space'
3032}
3133
3234export default class Table2CSVPlugin extends Plugin {
@@ -49,7 +51,7 @@ export default class Table2CSVPlugin extends Plugin {
4951 const viewMode = view . getMode ( ) ;
5052 if ( viewMode == "preview" ) {
5153 // Now convert the tables
52- const csvString = htmlToCSV ( view . previewMode . containerEl , this . settings . sepChar , this . settings . quoteData ) ;
54+ const csvString = htmlToCSV ( view . previewMode . containerEl , this . settings . sepChar , this . settings . quoteData , this . settings . removeCRLF ) ;
5355
5456 // TODO: prüfen, ob csvString leer oder nicht! Nur wenn nicht, Datei anlegen etc.
5557 if ( csvString . length > 0 ) {
@@ -108,6 +110,8 @@ export default class Table2CSVPlugin extends Plugin {
108110
109111 // This adds a settings tab so the user can configure various aspects of the plugin
110112 this . addSettingTab ( new Table2CSVSettingTab ( this . app , this ) ) ;
113+
114+ console . log ( `Table to CSV plugin: Version ${ this . manifest . version } loaded.` ) ;
111115 }
112116
113117 onunload ( ) {
@@ -123,29 +127,40 @@ export default class Table2CSVPlugin extends Plugin {
123127}
124128
125129
126- function htmlToCSV ( html : HTMLElement , sep : string , quote : boolean ) {
130+ function htmlToCSV ( html : HTMLElement , sep : string , quote : boolean , removeCRLF : string ) {
127131 var data = [ ] ;
128132 var table = html . querySelector ( "table" ) ;
129- console . log ( `htmlToCSV::table: ${ table } ` ) ;
133+ // console.log(`htmlToCSV::table: ${table}`);
130134
131135 if ( table ) {
132136 var rows = table . rows ;
133- console . log ( `htmlToCSV::rows: ${ rows } ` ) ;
137+ // console.log(`htmlToCSV::rows: ${rows}`);
134138 for ( var i = 0 ; i < rows . length ; i ++ ) {
135139 var row = [ ] , cols = rows [ i ] . querySelectorAll ( "td, th" ) ;
136140
137141 for ( var j = 0 ; j < cols . length ; j ++ ) {
138- if ( ! quote ) {
139- row . push ( ( cols [ j ] as HTMLElement ) . innerText ) ;
140- } else {
141- row . push ( '"' + ( cols [ j ] as HTMLElement ) . innerText + '"' ) ;
142+ var cellContent = ( cols [ j ] as HTMLElement ) . innerText ;
143+
144+ // handle the optional replacement of CR/LF characters:
145+ if ( removeCRLF == 'removeCRLF-clear' ) {
146+ cellContent = cellContent . replace ( / ( \r \n | \n | \r ) / gm, "" ) ;
147+ } else if ( removeCRLF == 'removeCRLF-space' ) {
148+ cellContent = cellContent . replace ( / ( \r \n | \n | \r ) / gm, " " ) ;
149+ } else if ( removeCRLF == 'removeCRLF-string1' ) {
150+ cellContent = cellContent . replace ( / ( \r \n | \n | \r ) / gm, "[CR]" ) ;
142151 }
152+
153+ // handle the quoting of data cells:
154+ // for now it's just the hard-coded character "
155+ if ( quote ) cellContent = '"' + cellContent + '"' ;
156+
157+ row . push ( cellContent ) ;
143158 }
144159
145160 data . push ( row . join ( sep ) ) ;
146161 }
147162 }
148- console . log ( `htmlToCSV::data.length: ${ data . length } ` ) ;
163+ // console.log(`htmlToCSV::data.length: ${data.length}`);
149164 if ( data . length > 0 )
150165 return data . join ( "\n" ) ;
151166 else
@@ -220,7 +235,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
220235
221236 new Setting ( containerEl )
222237 . setName ( 'Quote data' )
223- . setDesc ( 'Do you want quotation marks around each cell\'s data?' )
238+ . setDesc ( 'Do you want quotation marks (") around each cell\'s data?' )
224239 . addToggle ( toggle => toggle
225240 . setValue ( this . plugin . settings . quoteData )
226241 . onChange ( async ( value ) => {
@@ -239,5 +254,19 @@ class Table2CSVSettingTab extends PluginSettingTab {
239254 this . plugin . settings . saveToClipboardToo = value ;
240255 await this . plugin . saveSettings ( ) ;
241256 } ) ) ;
257+
258+ new Setting ( containerEl )
259+ . setName ( 'Handling of CR/LF in data' )
260+ . setDesc ( 'Chose how to handle the occurance of return and linefeed characters in data cells.' )
261+ . addDropdown ( dropdown => dropdown
262+ . addOption ( 'removeCRLF-clear' , 'Remove all CR & LF characters' )
263+ . addOption ( 'removeCRLF-space' , 'Replace all CR & LF characters with one space' )
264+ . addOption ( 'removeCRLF-string1' , 'Replace all CR & LF characters with string [CR]' )
265+ . setValue ( this . plugin . settings . removeCRLF )
266+ . onChange ( async ( value ) => {
267+ this . plugin . settings . removeCRLF = value ;
268+ await this . plugin . saveSettings ( ) ;
269+ } ) )
270+
242271 }
243272}
0 commit comments