From a15234fdef8516e8b0bb35406d927b7601802cb3 Mon Sep 17 00:00:00 2001 From: Julia Radzhabova Date: Thu, 28 Nov 2024 19:17:08 +0300 Subject: [PATCH 1/4] Detect delimiter when opening csv --- apps/common/main/lib/view/OpenDialog.js | 69 ++++++++++++++++--------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/apps/common/main/lib/view/OpenDialog.js b/apps/common/main/lib/view/OpenDialog.js index dec32ddbaa..4a4429ec20 100644 --- a/apps/common/main/lib/view/OpenDialog.js +++ b/apps/common/main/lib/view/OpenDialog.js @@ -146,6 +146,7 @@ define([ this.settings = _options.settings; this.api = _options.api; this.validatePwd = _options.validatePwd || false; + this.detectedDelimiter = false; _options.tpl = _.template(this.template)(_options); @@ -365,17 +366,18 @@ define([ } if (this.type == Common.Utils.importTextType.CSV || this.type == Common.Utils.importTextType.Paste || this.type == Common.Utils.importTextType.Columns || this.type == Common.Utils.importTextType.Data) { - var delimiter = this.settings && this.settings.asc_getDelimiter() ? this.settings.asc_getDelimiter() : 4, - delimiterChar = this.settings && this.settings.asc_getDelimiterChar() ? this.settings.asc_getDelimiterChar() : ''; - var value = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter" : "sse-settings-data-delimiter"); - if (value) { - value = parseInt(value); - if (!isNaN(value)) { - delimiter = value; - (delimiter===-1) && (delimiterChar = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter-char" : "sse-settings-data-delimiter-char") || ''); - } - } - + // var delimiter = this.settings && this.settings.asc_getDelimiter() ? this.settings.asc_getDelimiter() : 4, + // delimiterChar = this.settings && this.settings.asc_getDelimiterChar() ? this.settings.asc_getDelimiterChar() : ''; + // var value = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter" : "sse-settings-data-delimiter"); + // if (value) { + // value = parseInt(value); + // if (!isNaN(value)) { + // delimiter = value; + // (delimiter===-1) && (delimiterChar = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter-char" : "sse-settings-data-delimiter-char") || ''); + // } + // } + + var delimiter = ''; this.cmbDelimiter = new Common.UI.ComboBox({ el: $('#id-delimiters-combo', this.$window), style: 'width: 100px;', @@ -391,7 +393,7 @@ define([ editable: false, takeFocusOnClose: true }); - this.cmbDelimiter.setValue( delimiter); + this.cmbDelimiter.setValue(delimiter); this.cmbDelimiter.on('selected', _.bind(this.onCmbDelimiterSelect, this)); this.inputDelimiter = new Common.UI.InputField({ @@ -400,7 +402,7 @@ define([ maxLength: 1, validateOnChange: true, validateOnBlur: false, - value: delimiterChar + value: '' }); this.inputDelimiter.setVisible(delimiter===-1); this.inputDelimiter.on ('changing', _.bind(this.onInputCharChanging, this)); @@ -420,30 +422,47 @@ define([ var encoding = (this.cmbEncoding && !this.cmbEncoding.isDisabled()) ? this.cmbEncoding.getValue() : ((this.settings && this.settings.asc_getCodePage()) ? this.settings.asc_getCodePage() : 0); - var delimiter = this.cmbDelimiter ? this.cmbDelimiter.getValue() : null, - delimiterChar = (delimiter == -1) ? this.inputDelimiter.getValue() : null; - (delimiter == -1) && (delimiter = null); - - var options = new Asc.asc_CTextOptions(encoding, delimiter, delimiterChar); - if (this.separatorOptions) { - options.asc_setNumberDecimalSeparator(this.separatorOptions.decimal); - options.asc_setNumberGroupSeparator(this.separatorOptions.thousands); - options.asc_setTextQualifier(this.separatorOptions.qualifier); - } switch (this.type) { case Common.Utils.importTextType.CSV: case Common.Utils.importTextType.TXT: case Common.Utils.importTextType.Data: - this.api.asc_decodeBuffer(this.preview, options, _.bind(this.previewCallback, this)); + this.api.asc_decodeBuffer(this.preview, encoding, _.bind(this.textCallback, this)); break; case Common.Utils.importTextType.Paste: case Common.Utils.importTextType.Columns: - this.api.asc_TextImport(options, _.bind(this.previewCallback, this), this.type == Common.Utils.importTextType.Paste); + this.api.asc_TextImport(encoding, _.bind(this.textCallback, this), this.type == Common.Utils.importTextType.Paste); break; } }, + textCallback: function(text) { + var delimiter, + delimiterChar, + encoding = (this.cmbEncoding && !this.cmbEncoding.isDisabled()) ? this.cmbEncoding.getValue() : + ((this.settings && this.settings.asc_getCodePage()) ? this.settings.asc_getCodePage() : 0); + if (this.detectedDelimiter || this.type === Common.Utils.importTextType.TXT) { + delimiter = this.cmbDelimiter ? this.cmbDelimiter.getValue() : null; + delimiterChar = delimiter == -1 ? this.inputDelimiter.getValue() : null; + } else { + var res = this.api.asc_getCSVDelimiter(text); + text = res.text; + delimiter = res.delimiter; + delimiterChar = delimiter===-1 ? res.delimiterChar || '' : ''; + this.cmbDelimiter.setValue(delimiter); + this.inputDelimiter.setVisible(delimiter===-1); + this.inputDelimiter.setValue(delimiterChar); + this.detectedDelimiter = true; + } + var options = new Asc.asc_CTextOptions(encoding, delimiter, delimiterChar); + if (this.separatorOptions) { + options.asc_setNumberDecimalSeparator(this.separatorOptions.decimal); + options.asc_setNumberGroupSeparator(this.separatorOptions.thousands); + options.asc_setTextQualifier(this.separatorOptions.qualifier); + } + this.previewCallback(this.api.asc_parseText(text, options)); + }, + previewCallback: function(data) { if (!data || !data.length) return; From eb1b40b1d3c4ecc5906d358b1e0ac726eb68ae1e Mon Sep 17 00:00:00 2001 From: Julia Radzhabova Date: Thu, 28 Nov 2024 19:42:23 +0300 Subject: [PATCH 2/4] Fix text import --- apps/common/main/lib/view/OpenDialog.js | 2 +- apps/spreadsheeteditor/main/app/controller/DataTab.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/common/main/lib/view/OpenDialog.js b/apps/common/main/lib/view/OpenDialog.js index 4a4429ec20..3e1598c5be 100644 --- a/apps/common/main/lib/view/OpenDialog.js +++ b/apps/common/main/lib/view/OpenDialog.js @@ -184,7 +184,7 @@ define([ } else { this.initCodePages(); if (this.preview) { - (this.previewData) ? this.previewCallback(this.previewData) : this.updatePreview(); + (this.previewData) ? this.textCallback(this.previewData) : this.updatePreview(); } } if (this.type == Common.Utils.importTextType.Data) { diff --git a/apps/spreadsheeteditor/main/app/controller/DataTab.js b/apps/spreadsheeteditor/main/app/controller/DataTab.js index 5771d14bc6..e99a4eb9ff 100644 --- a/apps/spreadsheeteditor/main/app/controller/DataTab.js +++ b/apps/spreadsheeteditor/main/app/controller/DataTab.js @@ -217,7 +217,7 @@ define([ }, onTextToColumn: function() { - this.api.asc_TextImport(this._state.CSVOptions, _.bind(this.onTextToColumnCallback, this), false); + this.api.asc_TextImport(this._state.CSVOptions.asc_getCodePage(), _.bind(this.onTextToColumnCallback, this), false); }, onTextToColumnCallback: function(data) { From add40acb0d0ca96d59e83f39c4ac38ee2fc054e8 Mon Sep 17 00:00:00 2001 From: Julia Radzhabova Date: Thu, 28 Nov 2024 20:31:52 +0300 Subject: [PATCH 3/4] [SSE] Fix saving to csv --- apps/common/main/lib/view/OpenDialog.js | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/apps/common/main/lib/view/OpenDialog.js b/apps/common/main/lib/view/OpenDialog.js index 3e1598c5be..80d7b94684 100644 --- a/apps/common/main/lib/view/OpenDialog.js +++ b/apps/common/main/lib/view/OpenDialog.js @@ -366,18 +366,21 @@ define([ } if (this.type == Common.Utils.importTextType.CSV || this.type == Common.Utils.importTextType.Paste || this.type == Common.Utils.importTextType.Columns || this.type == Common.Utils.importTextType.Data) { - // var delimiter = this.settings && this.settings.asc_getDelimiter() ? this.settings.asc_getDelimiter() : 4, - // delimiterChar = this.settings && this.settings.asc_getDelimiterChar() ? this.settings.asc_getDelimiterChar() : ''; - // var value = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter" : "sse-settings-data-delimiter"); - // if (value) { - // value = parseInt(value); - // if (!isNaN(value)) { - // delimiter = value; - // (delimiter===-1) && (delimiterChar = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter-char" : "sse-settings-data-delimiter-char") || ''); - // } - // } - - var delimiter = ''; + var delimiter = '', + delimiterChar = ''; + if (!this.preview) { // don't need to detect delimiter (save to csv) + delimiter = this.settings && this.settings.asc_getDelimiter() ? this.settings.asc_getDelimiter() : 4, + delimiterChar = this.settings && this.settings.asc_getDelimiterChar() ? this.settings.asc_getDelimiterChar() : ''; + var value = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter" : "sse-settings-data-delimiter"); + if (value) { + value = parseInt(value); + if (!isNaN(value)) { + delimiter = value; + (delimiter===-1) && (delimiterChar = Common.localStorage.getItem(this.type == Common.Utils.importTextType.CSV ? "sse-settings-csv-delimiter-char" : "sse-settings-data-delimiter-char") || ''); + } + } + } + this.cmbDelimiter = new Common.UI.ComboBox({ el: $('#id-delimiters-combo', this.$window), style: 'width: 100px;', @@ -402,7 +405,7 @@ define([ maxLength: 1, validateOnChange: true, validateOnBlur: false, - value: '' + value: delimiterChar }); this.inputDelimiter.setVisible(delimiter===-1); this.inputDelimiter.on ('changing', _.bind(this.onInputCharChanging, this)); From c5fc846e9a7ba2fc2554b11797acd79bceed71ab Mon Sep 17 00:00:00 2001 From: Julia Radzhabova Date: Fri, 29 Nov 2024 19:18:28 +0300 Subject: [PATCH 4/4] [SSE] Fix custom delimiter --- apps/common/main/lib/view/OpenDialog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/common/main/lib/view/OpenDialog.js b/apps/common/main/lib/view/OpenDialog.js index 80d7b94684..1df02c8b81 100644 --- a/apps/common/main/lib/view/OpenDialog.js +++ b/apps/common/main/lib/view/OpenDialog.js @@ -450,7 +450,7 @@ define([ } else { var res = this.api.asc_getCSVDelimiter(text); text = res.text; - delimiter = res.delimiter; + delimiter = res.delimiter || -1; delimiterChar = delimiter===-1 ? res.delimiterChar || '' : ''; this.cmbDelimiter.setValue(delimiter); this.inputDelimiter.setVisible(delimiter===-1);