From 0560a9a403f20e43efeb1b7026620f61451d2a25 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 6 Jul 2021 16:09:30 +0200 Subject: [PATCH] Fix forms CSV export --- www/form/export.js | 25 +++++++++++++++++-------- www/form/inner.js | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/www/form/export.js b/www/form/export.js index 0e78d171a..4afc4170a 100644 --- a/www/form/export.js +++ b/www/form/export.js @@ -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] || '')); diff --git a/www/form/inner.js b/www/form/inner.js index 8e6b30d47..3fe71873d 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -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') },