-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathformsfromsheet.gs
205 lines (183 loc) · 13.3 KB
/
formsfromsheet.gs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function onOpen() {
var menuEntries = [];
menuEntries.push({name:"FORM", functionName: "CreateFormfromSheet"});
SpreadsheetApp.getActiveSpreadsheet().addMenu("SCRIPTS", menuEntries);
}
function CreateFormfromSheets() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sh = s.getActiveSheet();
// var s = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// for (var k=0; k<s.length; k++) {
// var sh = s[k];
var r = sh.getDataRange();
var n = r.getNumRows();
var d = r.getValues();
var folder = DriveApp.getFolderById(d[0][3]);
var createForm = DriveApp.getFileById(d[0][6]).makeCopy(d[0][1], folder).getId();
var form = FormApp.openById(createForm);
form.setDescription(d[1][1]);
for(var i=0;i<n;i++){
var id = d[i][0];
if (id=='CHECKBOX') {
var its = form.getItems();
for (var w = 0; w < its.length; w += 1){
var ite = its[w];
if (ite.getTitle() === "CH"){
var dup = ite.asCheckboxItem().duplicate();
var q = dup.setTitle(d[i][1]).setHelpText(d[i][2]).setRequired(true).setPoints(d[i][3]);
if (d[i][22] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false),q.createChoice(d[i][21], false),q.createChoice(d[i][22], false)]);}
else if (d[i][21] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false),q.createChoice(d[i][21], false)]);}
else if (d[i][20] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false)]);}
else if (d[i][19] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false)]);}
else if (d[i][18] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false)]);}
else if (d[i][17] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false)]);}
else if (d[i][16] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false)]);}
else if (d[i][15] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false)]);}
else if (d[i][14] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false)]);}
else if (d[i][13] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false)]);}
else if (d[i][12] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false)]);}
else if (d[i][11] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false)]);}
else if (d[i][10] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false)]);}
else if (d[i][9] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false)]);}
if (d[i][4] !== ""){
var correctFeedback = FormApp.createFeedback().setText(d[i][4]).build();
q.setFeedbackForCorrect(correctFeedback);
}
if (d[i][5] !== ""){
var incorrectFeedback = FormApp.createFeedback().setText(d[i][5]).addLink(d[i][6],d[i][7]).build();
q.setFeedbackForIncorrect(incorrectFeedback);
}
}}
}
else if (id=='LIST') {
var its = form.getItems();
for (var w = 0; w < its.length; w += 1){
var ite = its[w];
if (ite.getTitle() === "LI"){
var dup = ite.asListItem().duplicate();
var q = dup.setTitle(d[i][1]).setHelpText(d[i][2]).setRequired(true).setPoints(d[i][3]);
if (d[i][22] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false),q.createChoice(d[i][21], false),q.createChoice(d[i][22], false)]);}
else if (d[i][21] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false),q.createChoice(d[i][21], false)]);}
else if (d[i][20] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false),q.createChoice(d[i][20], false)]);}
else if (d[i][19] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false),
q.createChoice(d[i][19], false)]);}
else if (d[i][18] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false),q.createChoice(d[i][18], false)]);}
else if (d[i][17] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false),q.createChoice(d[i][17], false)]);}
else if (d[i][16] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false),q.createChoice(d[i][16], false)]);}
else if (d[i][15] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false),
q.createChoice(d[i][15], false)]);}
else if (d[i][14] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false),q.createChoice(d[i][14], false)]);}
else if (d[i][13] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false),q.createChoice(d[i][13], false)]);}
else if (d[i][12] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false),q.createChoice(d[i][12], false)]);}
else if (d[i][11] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false),
q.createChoice(d[i][11], false)]);}
else if (d[i][10] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false),q.createChoice(d[i][10], false)]);}
else if (d[i][9] !== undefined) {
q.setChoices([q.createChoice(d[i][8], true),q.createChoice(d[i][9], false)]);}
if (d[i][4] !== ""){
var correctFeedback = FormApp.createFeedback().setText(d[i][4]).build();
q.setFeedbackForCorrect(correctFeedback);
}
if (d[i][5] !== ""){
var incorrectFeedback = FormApp.createFeedback().setText(d[i][5]).addLink(d[i][6],d[i][7]).build();
q.setFeedbackForIncorrect(incorrectFeedback);
}
}}
}
else if (id=='TEXT') {
var txt = form.addTextItem().setTitle(d[i][1]).setHelpText(d[i][2]).setRequired(true).setPoints(d[i][3]);
var textValidation = FormApp.createTextValidation().requireTextMatchesPattern(d[i][8]).build();
txt.setValidation(textValidation);
if (d[i][5] != ""){
var feedBack = FormApp.createFeedback().setText(d[i][5]).addLink(d[i][6],d[i][7]).build();
txt.setGeneralFeedback(feedBack);
}
}
else if (id=='TITLE') {
form.addSectionHeaderItem().setTitle(d[i][1]).setHelpText(d[i][2]);
}
} //This is the for loop.
var iti = form.getItems();
for (var y = 0; y < iti.length; y += 1){
var ito = iti[y];
if (ito.getTitle() === "CH"){
form.deleteItem(ito);
}
else if (ito.getTitle() === "LI"){
form.deleteItem(ito);
}
}
// }
}