forked from teachduttonteach/GSuiteObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forms.gs
170 lines (157 loc) · 5.69 KB
/
Forms.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
var QUESTION_TYPE = function() {
this.PARAGRAPH = {
"code": 1,
"string": "Paragraph"
};
this.MULTIPLE_CHOICE = {
"code": 2,
"string": "Multiple Choice"
};
this.MULTIPLE_SELECT = {
"code": 3,
"string": "Multiple Select"
};
this.MC_GRID = {
"code": 4,
"string": "MC Grid"
};
this.MS_GRID = {
"code": 5,
"string": "MS Grid"
};
this.SHORT_ANSWER = {
"code": 6,
"string": "Short Answer"
};
this.TRUE_FALSE = {
"code": 7,
"string": "True / False"
};
};
var Form = function(id) {
this.form = null;
if (underscoreGS._isNumber(id)) {
this.form = FormApp.openById(id);
} else {
throw new Error("Form id needs to be defined in Form()");
}
if (null == this.form) throw new Error("Form not found in Form()");
this._ui = FormApp.getUi();
this.addMenu = function(menuName, itemName, functionName) {
if ((null != menuName) && (null != itemName) && (null != functionName)) {
this._ui.createMenu(menuName).addItem(itemName, functionName).addToUi();
return this;
} else {
throw new Error("menuName, itemName and functionName need to be defined for Form.addMenu");
}
};
this.addItem = function(title, row, questionType, optionsColumn, mcGridRowsColumn) {
checkVariables({"Title": {"null": title}, "Sheet": {"object": sheet}, "Row": {"int": row}, "Column": {"int": column}}, "Form.addItem");
switch (questionType) {
case bellworkForm.QUESTION_TYPES.PARAGRAPH["string"]:
this.form.addParagraphTextItem().setTitle(title);
break;
case bellworkForm.QUESTION_TYPES.TRUE_FALSE["string"]:
this.form.addMultipleChoiceItem().setTitle(title).setChoiceValues(["True", "False"]);
break;
case bellworkForm.QUESTION_TYPES.MULTIPLE_CHOICE["string"]:
this.form.addMultipleChoiceItem().setTitle(title);
this.addMultiple("choice", title, sheet.getValue(row, optionsColumn));
break;
case bellworkForm.QUESTION_TYPES.MULTIPLE_SELECT["string"]:
this.form.addCheckboxItem().setTitle(title);
this.addMultiple("select", title, sheet.getValue(row, optionsColumn));
break;
case bellworkForm.QUESTION_TYPES.MC_GRID["string"]:
var item = this.form.addGridItem().setTitle(title);
item.setColumns(this.form.convertLinebreaksToList(sheet.getValue(row, optionsColumn)));
item.setRows(this.form.convertLinebreaksToList(sheet.getValue(row, mcGridRowsColumn)));
break;
case bellworkForm.QUESTION_TYPES.MS_GRID["string"]:
var item = this.form.addCheckboxGridItem().setTitle(title);
item.setColumns(this.form.convertLinebreaksToList(sheet.getValue(row, optionsColumn)));
item.setRows(this.form.convertLinebreaksToList(sheet.getValue(row, mcGridRowsColumn)));
break;
default:
this.form.addParagraphTextItem().setTitle(title);
break;
}
};
this.addParagraph = function(title) {
if (null != title) {
this.form.addParagraphTextItem().setTitle(title);
return this;
} else {
throw new Error("Title needs to be defined for Form.addParagraph");
}
};
this.addTrueFalse = function(title) {
if (null != title) {
this.addMultiple("choice", title, ["True", "False"], false);
return this;
} else {
throw new Error("Title needs to be defined for Form.addTrueFalse");
}
};
this.setChoices = function(list) {
if (null != list) {
this.form.setChoices(list);
return this;
} else {
throw new Error("List and form need to be defined for Form.setChoices");
}
}
this.convertLinebreaksToList = function(list, form) {
if ((null != list) && (null != form)) {
var choices = list.split("\n");
var arrChoices = [];
for (var i = 0; i < choices.length; i++) {
arrChoices.push(form.createChoice(choices[i]));
}
return arrChoices;
} else {
throw new Error("List and form need to be defined for Form.convertLinebreaksToList");
}
};
this.addMultiple = function(type, title, items, convert) {
if ((null != type) && (null != title) && (null != items)) {
var mcItem = null;
if (type == "choice") mcItem = this.form.addMultipleChoiceItem().setTitle(title);
else mcItem = this.form.addCheckboxItem().setTitle(title);
if (convert) items = this.setChoices(this.convertLinebreaksToList(items, mcItem));
else mcItem.setChoiceValues(items);
return this;
} else {
throw new Error("Type, title and items need to be defined for Form.addMultiple");
}
};
this.addMultipleGrid = function(type, title, columns, rows) {
if ((null != type) && (null != title) && (null != columns) && (null != rows)) {
var mcItem = null;
if (type == "choice") mcItem = this.form.addGridItem().setTitle(title);
else mcItem = this.form.addCheckboxGridItem().setTitle(title);
mcItem.setColumns(columns);
mcItem.setRows(rows);
return this;
} else {
throw new Error("Type, title, columns and rows need to be defined for Form.addMultipleGrid");
}
};
this.addImage = function(file) {
this.addImageItem().setImage(file);
};
this.deleteItems = function() {
for (var i = this.form.getItems().length - 1; i >= 0; i--) {
this.form.deleteItem(i);
}
this.form.deleteAllResponses();
return this;
};
};
var FormEvent = function(event) {
this.title = e.source.getTitle();
this.date = e.response.getTimestamp();
this.email = e.response.getRespondentEmail();
this.response = e.response.getItemResponses()[0].getResponse();
this.fullDate = (this.date.getMonth() + 1) + "/" + this.date.getDate() + "\n" + e.response.getItemResponses()[0].getItem().getTitle();
}