我正在编写一个Form脚本,需要为每个问题存储额外的设置。我想避免创建电子表格,所以我尝试使用scriptDB来存储每个问题的设置。 (如果有更好的方法请告诉我。)
现在我正在尝试创建一个synchronizeDB()函数,该函数将使用任何新的或删除的问题更新数据库。我不能删除和重新加载,因为这会删除我为每个问题存储的额外数据。
-bj
答案 0 :(得分:0)
我明白了:
function snycFormToDb(){
var formQs = getFormQarray();
addDBQuestions(formQs);
removeDBQuestions(formQs);
// showAll();
}
// Save Questions To DB
function getFormQarray(){
var form = FormApp.getActiveForm();
var items = form.getItems();
var itemL = items.length-1;
var allQuestions = [];
for (var i=0; i <= itemL; ++i) {
var qID = items[i].getId();
var qType = items[i].getType()+ "";
var qTitle = items[i].getTitle();
var qIndex= items[i].getIndex();
var qHelpTxt = items[i].getHelpText();
var entry = [qID,qIndex,qTitle,qType,qHelpTxt];
allQuestions.push(entry);
} // end For
return(allQuestions);
}; // end function questionsDB()
// Add questions to DB
function addDBQuestions(formQs) {
var dbQuestions = [];
var results = db.query({});
while (results.hasNext()) {
var current = results.next();
dbQuestions.push(current);
}
var dbQuestionsL = dbQuestions.length-1;
var formQsL = formQs.length-1;
var addToDb = [];
for (var j=0; j <= formQsL; ++j) {
var notPresent = false;
for (var k=0; k <= dbQuestionsL; ++k) {
var dbID = dbQuestions[k]['qID'];
var frmID = formQs[j][0];
if (dbID == frmID){
notPresent = true;
}
}
if (notPresent == false) {
//var entry = [qID,qIndex,qTitle,qType,qHelpTxt];
//var format = {type: formQs[j][2],title: formQs[j][1],qID: formQs[j][0]};
var format = {qID: formQs[j][0],qIndex: formQs[j][1],qTitle: formQs[j][2],qType: formQs[j][3],qHelpTxt: formQs[j][4]};
addToDb.push(format);
}
} // end for formQsL
db.saveBatch(addToDb, false);
} //end my function
// Remove Q's from Database
function removeDBQuestions(formQs) {
var formQsL = formQs.length-1;
//get qID results from Database
var results = db.query({});
while (results.hasNext()) {
var current = results.next();
//var recordID = results.getId();
var deleteCurrent = "true";
// Delete missing questions
for (var j=0; j <= formQsL; ++j) {
var c1 = current.qID;
var c2 = formQs[j][0];
if (current.qID == formQs[j][0]){ deleteCurrent = "false";}
}
if (deleteCurrent == "true"){
db.remove(current);
}
}
} //end my function