Export form in sheets

pull/1/head
yflory 3 years ago
parent ca95af082d
commit 86aad06545

@ -145,9 +145,13 @@ define([
return metadataMgr.getNetfluxId() + '-' + privateData.clientId;
};
var getWindow = function () {
return window.frames && window.frames[0];
};
var getEditor = function () {
if (!window.frames || !window.frames[0]) { return; }
return window.frames[0].editor || window.frames[0].editorCell;
var w = getWindow();
if (!w) { return; }
return w.editor || w.editorCell;
};
var setEditable = function (state, force) {
@ -1224,6 +1228,73 @@ define([
});
};
APP.testArr = [
['a','b',1,'d'],
['e',undefined,'g','h']
];
var makePatch = APP.makePatch = function (arr) {
var w = getWindow();
if (!w) { return; }
// Define OO classes
var AscCommonExcel = w.AscCommonExcel;
var CellValueData = AscCommonExcel.UndoRedoData_CellValueData;
var CCellValue = AscCommonExcel.CCellValue;
//var History = w.AscCommon.History;
var AscCH = w.AscCH;
var Asc = w.Asc;
var UndoRedoData_CellSimpleData = AscCommonExcel.UndoRedoData_CellSimpleData;
var editor = getEditor();
var Id = editor.GetSheet(0).worksheet.Id;
//History.Create_NewPoint();
var patches = [];
arr.forEach(function (arr2, i) {
arr2.forEach(function (v, j) {
var obj = {};
if (typeof(v) === "string") { obj.text = v; obj.type = 1; }
else if (typeof(v) === "number") { obj.number = v; obj.type = 0; }
else { return; }
var newValue = new CellValueData(undefined, new CCellValue(obj));
var nCol = j;
var nRow = i;
var patch = new AscCommonExcel.UndoRedoItemSerializable(AscCommonExcel.g_oUndoRedoCell, AscCH.historyitem_Cell_ChangeValue, Id,
new Asc.Range(nCol, nRow, nCol, nRow),
new UndoRedoData_CellSimpleData(nRow, nCol, undefined, newValue), undefined);
patches.push(patch);
/*
History.Add(AscCommonExcel.g_oUndoRedoCell, AscCH.historyitem_Cell_ChangeValue, Id,
new Asc.Range(nCol, nRow, nCol, nRow),
new UndoRedoData_CellSimpleData(nRow, nCol, undefined, newValue), undefined, true);
*/
});
});
var oMemory = new w.AscCommon.CMemory();
var aRes = [];
patches.forEach(function (item) {
editor.GetSheet(0).worksheet.workbook._SerializeHistoryBase64(oMemory, item, aRes);
});
// Make the patch
var msg = {
type: "saveChanges",
changes: parseChanges(JSON.stringify(aRes)),
changesIndex: ooChannel.cpIndex || 0,
locks: getUserLock(getId(), true),
excelAdditionalInfo: null
};
// Send the patch
rtChannel.sendMsg(msg, null, function (err, hash) {
if (err) {
return void console.error(err);
}
// Apply it on our side
ooChannel.send(msg);
ooChannel.lastHash = hash;
ooChannel.cpIndex++;
});
};
var makeChannel = function () {
var msgEv = Util.mkEvent();

@ -13,9 +13,10 @@ define([
value += '"' + vv + '"';
return value;
};
Export.results = function (content, answers, TYPES) {
Export.results = function (content, answers, TYPES, isArray) {
if (!content || !content.form) { return; }
var csv = "";
var array = [];
var form = content.form;
var questions = [Messages.form_poll_time, Messages.share_formView];
@ -35,6 +36,7 @@ define([
if (i) { csv += ','; }
csv += escapeCSV(v);
});
array.push(questions);
Object.keys(answers || {}).forEach(function (key) {
var obj = answers[key];
@ -42,21 +44,26 @@ define([
var time = new Date(obj.time).toISOString();
var msg = obj.msg || {};
var user = msg._userdata || {};
csv += escapeCSV(time);
csv += ',' + escapeCSV(user.name || Messages.anonymous);
var line = [];
line.push(time);
line.push(user.name || Messages.anonymous);
content.order.forEach(function (key) {
var type = form[key].type;
if (!TYPES[type]) { return; } // Ignore static types
if (TYPES[type].exportCSV) {
var res = TYPES[type].exportCSV(msg[key], form[key]).map(function (str) {
return escapeCSV(str);
}).join(',');
csv += ',' + res;
var res = TYPES[type].exportCSV(msg[key], form[key]);
Array.prototype.push.apply(line, res);
return;
}
csv += ',' + escapeCSV(String(msg[key] || ''));
line.push(String(msg[key] || ''));
});
line.forEach(function (v, i) {
if (i) { csv += ','; }
csv += escapeCSV(v);
});
array.push(line);
});
if (isArray) { return array; }
return csv;
};

@ -2637,6 +2637,19 @@ define([
}), title);
});
// Export in "sheet"
Messages.form_exportSheet = "Export in spreadsheet"; // XXX
var export2Button = h('button.btn.btn-primary', [
h('i.fa.fa-download'),
Messages.form_exportSheet
]);
$(export2Button).appendTo($controls);
$(export2Button).click(function () {
var arr = Exporter.results(content, answers, TYPES, true);
if (!arr) { return void UI.warn(Messages.error); }
console.error(arr);
});
var summary = true;
var form = content.form;

Loading…
Cancel
Save