From 574726cf9d1ffc36f56e8fab81a994e6f947b387 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 21 Jan 2020 00:12:27 +0100 Subject: [PATCH] Handle parsing of cells with quotes and tabs inside --- src/index.html | 2 +- src/main.js | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/index.html b/src/index.html index 3eef0e6..d30589a 100644 --- a/src/index.html +++ b/src/index.html @@ -33,7 +33,7 @@ Title:
Paste your questions from a spreadsheet here:
- +
diff --git a/src/main.js b/src/main.js index 004015f..de724b5 100644 --- a/src/main.js +++ b/src/main.js @@ -159,7 +159,7 @@ function startSubmitTimer(time) { function parseLine(line) { - // replace newline markers with newlines + // replace newline markers with html newlines var els = line.replace(/⎊/g, '
').split('\t') if (els.length < 4) { @@ -194,7 +194,23 @@ function parseSpreadsheet() { var textEl = document.getElementById('questions') - var text = textEl.value.replace(/"([^"]|"")+"/g, m => m.replace(/\n/g, '⎊').replace(/""/g, '"').replace(/^"(.*)"$/, '$1')) + /* MAGIC: + * 1. Find quoted multiline cells ("ab") + * 2. Replace all newlines inside with a special char so the cell won't be + * split later ("ab") + * 3. Replace escaped quotes ("a ""b"" c") with single ones ("a "b" c") + * 4. Remove the outer quotes and tabs (ab) + * 5. Replace all tabs inside with spaces + * 6. Re-add outer tabs (ab) + */ + + var text = textEl.value.replace(/\t"([^"\n]|"")+\n([^"]|"")+"\t/g, + m => m.replace(/\n/g, '⎊') + .replace(/""/g, '"') + .replace(/^\t"(.*)"\t$/, '$1') + .replace(/\t/g, ' ') + .replace(/^(.*)$/g, '\t$1\t') + ) state.questions = text.split('\n').map(parseLine) @@ -293,15 +309,22 @@ function createQuiz() { console.log('Creating new quiz') + state.title = document.getElementById('titleField').value + if (state.title == '') { + alert('Please enter a title') + return + } + clearState() removeLink() - if (parseSpreadsheet().success == false) + if (parseSpreadsheet().success == false) { + console.log('Quiz creation failed.') return + } console.log('Spreadsheet parsing successful') - state.title = document.getElementById('titleField').value updateTitles() changeView('prescreen') @@ -455,7 +478,7 @@ const memes = [ 'Eine Runde Kicker?', 'Hulkdrian!', 'Jetz\' bin i\' wieda doa', - 'Mmmmh Carbonstaub :P', + 'Mmmmh Carbonstaub', '#würthshausfranz', '#berlinerluft', 'FaSTTUBe, not FastCOCUE', @@ -473,14 +496,14 @@ const memes = [ 'Wer AMS sagt muss auch BMS sagen', 'Ist der Kabelbinder in der BOM?', '*Fistbump*', - 'Nividia', + 'Nividia!', 'Ihr schafft das! :)', 'Klotzen, nicht kleckern!', '*revving noises*', 'Resistance is futile', 'Jan schweißt das noch', 'Would Claude approve of this?', - 'Im CAD hat\'s gepasst', + 'Im CAD hat\'s noch gepasst', '¯\\_(ツ)_/¯', 'AMK Brudi', 'Mmmhh cones',