如何正确将Google插件提取的表单数据存储到Firebase Realtime Database
Google表单提取插件Firebase存储格式异常问题
问题背景
我正在开发一款可提取表单信息的Google插件,目前已成功抓取数据并存储为JSON对象,但将结果上传到Firebase Realtime Database时存储格式异常。我期望的存储结构和实际异常存储效果可参考对应截图。
初始代码
以下是我最初编写的生成JSON对象的代码:
function makeJSON(form_title, ques_type, form_question, number_of_choices, multi_options, multi_answer, checkBox_options, checkBox_answer, number_of_questions) { let type_of_question = ques_type.split("~ "); let questions = form_question.split("~ "); let mult_options = multi_options.split("~ "); let mult_answer = multi_answer.split("~ "); let check_options = checkBox_options.split("~ "); let check_answer = checkBox_answer.split("~ "); let multi_answer_list = []; let checkbox_answer_list = []; var newDate = new Date(); var dateAdded = newDate.toLocaleString("en-US"); let jsonObj = "{"; // 将表单数据格式化存储到json对象中 for(var i = 0; i < number_of_questions; i++){ if(i < number_of_questions){ var count = number_of_choices[i] jsonObj += '"' + [i+1] + '"' + ":{" + '"question-type"' + ":" + '"' + type_of_question[i] + '",' + '"question"' + ":" + '"' + questions[i] + '",'; for (var k = 0; k < count; k++) { if(type_of_question[i] === "Multiple Choice"){ jsonObj += '"' + "option" + [k+1] + '"' + ":" + '"' + mult_options[k] + '"' + ","; } if(type_of_question[i] === "CheckBox"){ jsonObj += '"' + "option" + [k+1] + '"' + ":" + '"' + check_options[k] + '"' + ","; } } for (var j = 0; j < count; j++) { if(type_of_question[i] === "Multiple Choice"){ if(mult_answer[j].trim().toLowerCase() === 'true'){ multi_answer_list.push(mult_options[j]); //jsonObj += '"' + "answer" + [j+1] + '"' + ":" + '"' + mult_options[j] + '"' + ","; } } if(type_of_question[i] === "CheckBox"){ if(check_answer[j].trim().toLowerCase() === 'true'){ checkbox_answer_list.push(check_options[j]); //jsonObj += '"' + "answer" + [j+1] + '"' + ":" + '"' + check_options[j] + '"' + ","; } } } if(i < number_of_questions){ if(type_of_question[i] === "Multiple Choice"){ jsonObj += '"' + "answer" + '"' + ":" + '"' + multi_answer_list.join(",") + '"' + ","; } if(type_of_question[i] === "CheckBox"){ jsonObj += '"' + "answer" + '"' + ":" + '"' + checkbox_answer_list.join(",") + '"' + ","; } } if(type_of_question[i] !== "Multiple Choice" && type_of_question[i] !== "CheckBox"){ jsonObj += '"' + "answer" + '"' + ":" + '"false"' + ","; // 默认值 } if(i < number_of_questions-1){ jsonObj += '"' + "date added" + '"' + ":" + '"' + dateAdded + '"' + "},"; // 默认值 } else{ jsonObj += '"' + "date added" + '"' + ":" + '"' + dateAdded + '"' + "}"; // 默认值 } } } jsonObj += "}"; console.log(jsonObj); sendExtractedData(form_title, jsonObj) }
优化方案
我最终选择的实现方案使用了push()方法,虽未完全实现预期的存储结构,但已接近预期效果,优化后代码如下:
function makeJSON(form_title, type_of_question, questions, multi_options, multi_answer, checkBox_options, checkBox_answer, number_of_questions) { var dateAdded = new Date().toLocaleString("en-US"), quesList; let multiple_option = [], checkbox_option = [], multiple_answer = [], checkbox_answer = [], constructedJSON = {}; var uniqueQuizID = Utilities.getUuid(), num, x; /** 生成选项和答案列表 **/ for(var k = 0; k < multi_options.length; k++){ multiple_option.push(multi_options[k]); if(multi_answer[k]){ multiple_answer.push(multi_options[k]); } } for(var j = 0; j < checkBox_options.length; j++){ checkbox_option.push(checkBox_options[j]); if(check_answer[j]){ checkbox_answer.push(check_options[j]); } } /****/ console.log(uniqueQuizID); constructedJSON.quiz_title = form_title; // 将表单数据格式化存储到json对象中 for(var i = 0; i < number_of_questions; i++){ if(type_of_question[i].trim() === "Multiple Choice"){ quesList = {"item-type":type_of_question[i], "question":questions[i], "question-options":multiple_option, "question-responses":multiple_answer, "question-submitted":dateAdded}; x= i+1; num = ''+ x; constructedJSON[num] = quesList; } else if(type_of_question[i].trim() === "CheckBox"){ quesList = {"item-type":type_of_question[i], "question":questions[i], "question-options":checkbox_option, "question-responses":checkbox_answer, "question-submitted":dateAdded}; x= i+1; num = ''+ x; constructedJSON[num] = quesList; } else{ quesList = {"item-type":type_of_question[i], "question":questions[i], "question-submitted":dateAdded}; x= i+1; num = ''+ x; constructedJSON[num] = quesList; } } //console.log(constructedJSON); sendExtractedData(uniqueQuizID, constructedJSON); }
内容的提问来源于stack exchange,提问作者Roberto Flores
相关产品推荐
相关产品推荐

