Handle parsing of cells with quotes and tabs inside

This commit is contained in:
Oskar Winkels 2020-01-21 00:12:27 +01:00 committed by Oskar
parent d69b255497
commit 574726cf9d
2 changed files with 31 additions and 8 deletions

View File

@ -33,7 +33,7 @@
Title:<br> Title:<br>
<input type="text" id="titleField" required> <input type="text" id="titleField" required>
Paste your questions from a spreadsheet here:<br> Paste your questions from a spreadsheet here:<br>
<textarea rows="20" id="questions" required></textarea> <textarea rows="16" id="questions" required></textarea>
<br> <br>
<input type="button" value="Create Quiz" onclick="createQuiz()"> <input type="button" value="Create Quiz" onclick="createQuiz()">

View File

@ -159,7 +159,7 @@ function startSubmitTimer(time) {
function parseLine(line) { function parseLine(line) {
// replace newline markers with newlines // replace newline markers with html newlines
var els = line.replace(/⎊/g, '<br>').split('\t') var els = line.replace(/⎊/g, '<br>').split('\t')
if (els.length < 4) { if (els.length < 4) {
@ -194,7 +194,23 @@ function parseSpreadsheet() {
var textEl = document.getElementById('questions') 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 (<tab>"a<newline>b"<tab>)
* 2. Replace all newlines inside with a special char so the cell won't be
* split later (<tab>"a<specialchar>b"<tab>)
* 3. Replace escaped quotes ("a ""b"" c") with single ones ("a "b" c")
* 4. Remove the outer quotes and tabs (a<specialchar>b)
* 5. Replace all tabs inside with spaces
* 6. Re-add outer tabs (<tab>a<specialchar>b<tab>)
*/
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) state.questions = text.split('\n').map(parseLine)
@ -293,15 +309,22 @@ function createQuiz() {
console.log('Creating new quiz') console.log('Creating new quiz')
state.title = document.getElementById('titleField').value
if (state.title == '') {
alert('Please enter a title')
return
}
clearState() clearState()
removeLink() removeLink()
if (parseSpreadsheet().success == false) if (parseSpreadsheet().success == false) {
console.log('Quiz creation failed.')
return return
}
console.log('Spreadsheet parsing successful') console.log('Spreadsheet parsing successful')
state.title = document.getElementById('titleField').value
updateTitles() updateTitles()
changeView('prescreen') changeView('prescreen')
@ -455,7 +478,7 @@ const memes = [
'Eine Runde Kicker?', 'Eine Runde Kicker?',
'Hulkdrian!', 'Hulkdrian!',
'Jetz\' bin i\' wieda doa', 'Jetz\' bin i\' wieda doa',
'Mmmmh Carbonstaub :P', 'Mmmmh Carbonstaub',
'#würthshausfranz', '#würthshausfranz',
'#berlinerluft', '#berlinerluft',
'FaST<b>TUBe</b>, not Fast<b>COCUE</b>', 'FaST<b>TUBe</b>, not Fast<b>COCUE</b>',
@ -473,14 +496,14 @@ const memes = [
'Wer AMS sagt muss auch BMS sagen', 'Wer AMS sagt muss auch BMS sagen',
'Ist der Kabelbinder in der BOM?', 'Ist der Kabelbinder in der BOM?',
'*Fistbump*', '*Fistbump*',
'Nividia', 'Nividia!',
'Ihr schafft das! :)', 'Ihr schafft das! :)',
'Klotzen, nicht kleckern!', 'Klotzen, nicht kleckern!',
'*revving noises*', '*revving noises*',
'Resistance is futile', 'Resistance is futile',
'Jan schweißt das noch', 'Jan schweißt das noch',
'Would Claude approve of this?', 'Would Claude approve of this?',
'Im CAD hat\'s gepasst', 'Im CAD hat\'s noch gepasst',
'¯\\_(ツ)_/¯', '¯\\_(ツ)_/¯',
'AMK Brudi', 'AMK Brudi',
'Mmmhh cones', 'Mmmhh cones',