Fix forms CSV export

pull/1/head
yflory 4 years ago
parent 9bb884cac6
commit 0560a9a403

@ -18,13 +18,18 @@ define([
var csv = "";
var form = content.form;
var questions = Object.keys(form).map(function (key) {
var questions = [Messages.form_poll_time, Messages.share_formView];
content.order.forEach(function (key) {
var obj = form[key];
if (!obj) { return; }
return obj.q || Messages.form_default;
}).filter(Boolean);
questions.unshift(Messages.share_formView); // "Participant"
questions.unshift(Messages.form_poll_time); // "Time"
var type = obj.type;
if (!TYPES[type]) { return; } // Ignore static types
var c;
if (TYPES[type] && TYPES[type].exportCSV) { c = TYPES[type].exportCSV(false, obj); }
if (!c) { c = [obj.q || Messages.form_default]; }
Array.prototype.push.apply(questions, c);
});
questions.forEach(function (v, i) {
if (i) { csv += ','; }
@ -39,10 +44,14 @@ define([
var user = msg._userdata || {};
csv += escapeCSV(time);
csv += ',' + escapeCSV(user.name || Messages.anonymous);
Object.keys(form).forEach(function (key) {
content.order.forEach(function (key) {
var type = form[key].type;
if (TYPES[type] && TYPES[type].exportCSV) {
csv += ',' + escapeCSV(TYPES[type].exportCSV(msg[key]));
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;
return;
}
csv += ',' + escapeCSV(String(msg[key] || ''));

@ -1215,6 +1215,20 @@ define([
return h('div.cp-form-results-type-radio', results);
},
exportCSV: function (answer, form) {
var opts = form.opts;
var q = form.q || Messages.form_default;
if (answer === false) {
return (opts.items || []).map(function (obj) {
return q + ' | ' + obj.v;
});
}
if (!answer) { return ['']; }
return (opts.items || []).map(function (obj) {
var uid = obj.uid;
return String(answer[uid] || '');
});
},
icon: h('i.cptools.cptools-form-grid-radio')
},
checkbox: {
@ -1429,6 +1443,20 @@ define([
return h('div.cp-form-results-type-radio', results);
},
exportCSV: function (answer, form) {
var opts = form.opts;
var q = form.q || Messages.form_default;
if (answer === false) {
return (opts.items || []).map(function (obj) {
return q + ' | ' + obj.v;
});
}
if (!answer) { return ['']; }
return (opts.items || []).map(function (obj) {
var uid = obj.uid;
return String(answer[uid] || '');
});
},
icon: h('i.cptools.cptools-form-grid-check')
},
sort: {
@ -1650,13 +1678,14 @@ define([
return h('div.cp-form-type-poll', lines);
},
exportCSV: function (answer) {
if (!answer || !answer.values) { return ''; }
if (answer === false) { return; }
if (!answer || !answer.values) { return ['']; }
var str = '';
Object.keys(answer.values).sort().forEach(function (k, i) {
if (i !== 0) { str += ';'; }
str += k.replace(';', '').replace(':', '') + ':' + answer.values[k];
});
return str;
return [str];
},
icon: h('i.cptools.cptools-form-poll')
},

Loading…
Cancel
Save