Skip to content

Commit

Permalink
Merge pull request 'feature/csv-delimiter' (#199) from feature/csv-de…
Browse files Browse the repository at this point in the history
…limiter into release/v8.3.0
  • Loading branch information
Julia Radzhabova committed Dec 20, 2024
2 parents 3085760 + c5fc846 commit 8c5ac01
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
64 changes: 43 additions & 21 deletions apps/common/main/lib/view/OpenDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -183,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) {
Expand Down Expand Up @@ -365,14 +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,
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") || '');
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") || '');
}
}
}

Expand All @@ -391,7 +396,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({
Expand Down Expand Up @@ -420,30 +425,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 || -1;
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;

Expand Down
2 changes: 1 addition & 1 deletion apps/spreadsheeteditor/main/app/controller/DataTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8c5ac01

Please sign in to comment.