Fix forms CSV export

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

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

@ -1215,6 +1215,20 @@ define([
return h('div.cp-form-results-type-radio', results); 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') icon: h('i.cptools.cptools-form-grid-radio')
}, },
checkbox: { checkbox: {
@ -1429,6 +1443,20 @@ define([
return h('div.cp-form-results-type-radio', results); 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') icon: h('i.cptools.cptools-form-grid-check')
}, },
sort: { sort: {
@ -1650,13 +1678,14 @@ define([
return h('div.cp-form-type-poll', lines); return h('div.cp-form-type-poll', lines);
}, },
exportCSV: function (answer) { exportCSV: function (answer) {
if (!answer || !answer.values) { return ''; } if (answer === false) { return; }
if (!answer || !answer.values) { return ['']; }
var str = ''; var str = '';
Object.keys(answer.values).sort().forEach(function (k, i) { Object.keys(answer.values).sort().forEach(function (k, i) {
if (i !== 0) { str += ';'; } if (i !== 0) { str += ';'; }
str += k.replace(';', '').replace(':', '') + ':' + answer.values[k]; str += k.replace(';', '').replace(':', '') + ':' + answer.values[k];
}); });
return str; return [str];
}, },
icon: h('i.cptools.cptools-form-poll') icon: h('i.cptools.cptools-form-poll')
}, },

Loading…
Cancel
Save