-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynFormScript.js
78 lines (54 loc) · 1.96 KB
/
dynFormScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function myFunction() {
var form = FormApp.getActiveForm();
//Clear form
var prevQns = form.getItems();
form.deleteItem(prevQns[0]);
// Form is cleared
// Obtain the list of questions stored in a spreadsheet
var ss = SpreadsheetApp.openById(/*GSheet ID goes here*/);
var qnSheet = ss.getSheetByName("Questions");
qnSheet.activate();
//Open another spreadsheet to store responses to questions
var ss1 = SpreadsheetApp.openById(/*GSheet ID goes here*/);
Logger.log("Sheet No. %s opened.", qnSheet.getSheetId());
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss1.getId());
var ui = FormApp.getUi();
var qnArray = [];
var newQn = false;
// TODO : clean up the question prompt, make it more intuitive
do{
var response = ui.prompt("New Question", "Enter a question, or skip to form(Yes to add another, No to stop adding, Cancel to skip)", ui.ButtonSet.YES_NO_CANCEL);
if(response.getSelectedButton() == ui.Button.YES)
{
qnArray.push(response.getResponseText());
newQn = true;
}
if(response.getSelectedButton() == ui.Button.NO)
{
qnArray.push(response.getResponseText());
newQn = false;
}
if(response.getSelectedButton() == ui.Button.CANCEL)
{
break;
}
}while(newQn);
Logger.log(qnArray);
Logger.log(qnArray.length);
for(var j = 0; j< qnArray.length ; j++){
qnSheet.getRange(ss.getLastRow()+1,1,1,1).setValue(qnArray[j]);
}
qnSheet.autoResizeColumn(1);
var savedQnsRange = qnSheet.getDataRange();
Logger.log(savedQnsRange);
var savedQns = savedQnsRange.getValues();
Logger.log(savedQns);
var qnItem = form.addCheckboxItem();
qnItem.setTitle('Select (maximum 20) questions');
qnItem.setChoiceValues(savedQns);
var qnItemValidation = FormApp.createCheckboxValidation()
.requireSelectAtMost(20)
.setHelpText("Select at most 20 questions")
.build();
qnItem.setValidation(qnItemValidation);
}