From c7ac75d137b7a508a6cd4fb7ca43fdbfdc2b83c7 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 30 Jun 2021 12:47:02 +0200 Subject: [PATCH] Fix CSV export in forms --- www/form/export.js | 8 ++++++-- www/form/inner.js | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/www/form/export.js b/www/form/export.js index 93f3ff6d3..0e78d171a 100644 --- a/www/form/export.js +++ b/www/form/export.js @@ -5,7 +5,7 @@ define([ var Export = {}; var escapeCSV = function (v) { - if (!/("|,|\n)/.test(v)) { + if (!/("|,|\n|;)/.test(v)) { return v || ''; } var value = ''; @@ -40,7 +40,11 @@ define([ csv += escapeCSV(time); csv += ',' + escapeCSV(user.name || Messages.anonymous); Object.keys(form).forEach(function (key) { - if (msg[key] && typeof(msg[key]) !== "string") { console.warn(key, msg[key]); } + var type = form[key].type; + if (TYPES[type] && TYPES[type].exportCSV) { + csv += ',' + escapeCSV(TYPES[type].exportCSV(msg[key])); + return; + } csv += ',' + escapeCSV(String(msg[key] || '')); }); }); diff --git a/www/form/inner.js b/www/form/inner.js index 123e86d5b..8b51e2102 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -1646,6 +1646,15 @@ define([ return h('div.cp-form-type-poll', lines); }, + exportCSV: function (answer) { + 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; + }, icon: h('i.cptools.cptools-form-poll') }, };