-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcode.gs
More file actions
328 lines (288 loc) · 9.77 KB
/
code.gs
File metadata and controls
328 lines (288 loc) · 9.77 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Serves the HTML Web App.
*/
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index')
.setTitle('Doc2Form — AI Form Generator')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
/**
* Returns prompt templates to the frontend.
*/
function getPromptTemplates() {
return PROMPT_TEMPLATES;
}
/**
* Main entry point — generates a Google Form from text and/or file data.
* Returns edit URL and published URL on success.
*/
function generateFormSmart(rawText, fileData) {
try {
var formData = callGemini(rawText, fileData);
validateFormData(formData);
var urls = createGoogleForm(formData);
return { success: true, editUrl: urls.editUrl, publishedUrl: urls.publishedUrl, title: formData.title };
} catch (e) {
Logger.log('generateFormSmart error: ' + e.message);
return { success: false, error: e.message };
}
}
/**
* Validates the parsed Gemini response before building a form.
*/
function validateFormData(data) {
if (!data || typeof data !== 'object') {
throw new Error('Gemini returned invalid data. Please try again.');
}
if (!data.title || typeof data.title !== 'string') {
throw new Error('Form title is missing from AI response.');
}
if (!Array.isArray(data.questions) || data.questions.length === 0) {
throw new Error('No questions found in AI response. Try being more specific.');
}
var validTypes = [
'SHORT_ANSWER', 'PARAGRAPH', 'MULTIPLE_CHOICE', 'CHECKBOX',
'DROPDOWN', 'LINEAR_SCALE', 'DATE', 'TIME',
'MULTIPLE_CHOICE_GRID', 'CHECKBOX_GRID', 'SECTION_HEADER'
];
data.questions.forEach(function(q, i) {
if (!q.title) throw new Error('Question ' + (i + 1) + ' is missing a title.');
if (q.type && validTypes.indexOf(q.type) === -1) {
q.type = 'SHORT_ANSWER';
}
});
}
/**
* Google Forms ScaleItem: lower must be 0 or 1; upper must be 3–10 (inclusive). See setBounds docs.
*/
function clampScaleBounds(low, high) {
var lo = Math.round(Number(low));
var hi = Math.round(Number(high));
if (!isFinite(lo) || (lo !== 0 && lo !== 1)) {
lo = 1;
}
if (!isFinite(hi)) {
hi = 5;
}
if (hi < 3) {
hi = 3;
}
if (hi > 10) {
hi = 10;
}
if (hi <= lo) {
hi = Math.min(10, Math.max(3, lo + 2));
}
return [lo, hi];
}
/** Non-empty trimmed strings from an array (Gemini may omit, duplicate, or use numbers). */
function sanitizeStringArray(arr) {
if (!arr || !Array.isArray(arr)) {
return [];
}
return arr.map(function(s) {
return String(s == null ? '' : s).trim();
}).filter(function(s) {
return s.length > 0;
});
}
/** Multiple choice / checkbox / dropdown need at least 2 choices in practice; never pass empty to setChoiceValues. */
function ensureChoiceOptions(options, fallbackPrefix) {
var opts = sanitizeStringArray(options);
var prefix = fallbackPrefix || 'Option';
while (opts.length < 2) {
opts.push(prefix + ' ' + (opts.length + 1));
}
return opts;
}
/** Grid setRows / setColumns throw on empty arrays; both axes are required. */
function ensureGridAxes(rows, columns) {
var r = sanitizeStringArray(rows);
var c = sanitizeStringArray(columns);
if (r.length === 0) {
r = ['Row 1', 'Row 2'];
}
if (c.length === 0) {
c = ['A', 'B', 'C'];
}
return { rows: r, columns: c };
}
/**
* Calls Gemini API. Handles text-only, PDF (multimodal), and Word (pre-extracted text).
*/
function callGemini(text, fileData) {
var apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
if (!apiKey) {
throw new Error('GEMINI_API_KEY not set. Go to Project Settings → Script Properties and add it.');
}
var model = 'gemini-2.5-flash';
var url = 'https://generativelanguage.googleapis.com/v1beta/models/' + model + ':generateContent?key=' + apiKey;
var contentParts = [];
if (fileData) {
contentParts.push({
inline_data: {
mime_type: fileData.mimeType,
data: fileData.data
}
});
}
var finalText = text ? text.substring(0, 500000) : '';
contentParts.push({
text: 'Create a Google Form structure from the following content.\n' + finalText
});
var payload = {
contents: [{ parts: contentParts }],
system_instruction: {
parts: [{
text: GEMINI_SYSTEM_PROMPT
}]
},
generationConfig: { response_mime_type: 'application/json' }
};
var options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
var statusCode = response.getResponseCode();
if (statusCode === 429) {
throw new Error('API rate limit reached. Please wait a moment and try again.');
}
if (statusCode >= 500) {
throw new Error('Gemini API is temporarily unavailable (HTTP ' + statusCode + '). Try again shortly.');
}
var jsonResponse = JSON.parse(response.getContentText());
if (jsonResponse.error) {
throw new Error('Gemini API error: ' + jsonResponse.error.message);
}
if (!jsonResponse.candidates || !jsonResponse.candidates[0] ||
!jsonResponse.candidates[0].content || !jsonResponse.candidates[0].content.parts) {
throw new Error('Unexpected response from Gemini. The model may have refused the request.');
}
var raw = jsonResponse.candidates[0].content.parts[0].text;
try {
return JSON.parse(raw);
} catch (parseErr) {
throw new Error('Could not parse AI response as JSON. Try again or shorten the document.');
}
}
/**
* System prompt for Gemini — defines the JSON schema for form generation.
*/
var GEMINI_SYSTEM_PROMPT = [
'You are a Google Form generator. Your job is to create well-structured, thoughtful forms.',
'Output strict JSON only. No markdown, no explanation.',
'',
'JSON Schema:',
'{',
' "title": "Form Title",',
' "description": "Optional form description",',
' "questions": [',
' {',
' "title": "Question text",',
' "helpText": "Optional hint shown below the question",',
' "type": "SHORT_ANSWER | PARAGRAPH | MULTIPLE_CHOICE | CHECKBOX | DROPDOWN | LINEAR_SCALE | DATE | TIME | MULTIPLE_CHOICE_GRID | CHECKBOX_GRID | SECTION_HEADER",',
' "required": true,',
' "options": ["Option 1", "Option 2"],',
' "scaleMin": 1,',
' "scaleMax": 5,',
' "scaleMinLabel": "Not at all",',
' "scaleMaxLabel": "Very much",',
' "rows": ["Row 1", "Row 2"],',
' "columns": ["Col 1", "Col 2"]',
' }',
' ]',
'}',
'',
'Rules:',
'- "options" only for MULTIPLE_CHOICE, CHECKBOX, DROPDOWN',
'- "scaleMin", "scaleMax", "scaleMinLabel", "scaleMaxLabel" only for LINEAR_SCALE',
'- For LINEAR_SCALE: scaleMin must be 0 or 1 only; scaleMax must be an integer from 3 to 10 (Google Forms rule)',
'- "rows" and "columns" only for MULTIPLE_CHOICE_GRID and CHECKBOX_GRID',
'- Grids must each have at least two non-empty row labels and two non-empty column labels',
'- SECTION_HEADER creates a visual page/section break with title and optional helpText as description',
'- Default "required" to true for important fields, false for optional ones',
'- Choose the most appropriate question type for each field',
'- For documents: extract ALL fields faithfully, preserving the original structure',
'- For text prompts: create a smart, well-organized form matching the user\'s intent',
'- Always include at least a title and one question'
].join('\n');
/**
* Builds a Google Form from structured data and returns both URLs.
*/
function createGoogleForm(data) {
var form = FormApp.create(data.title || 'AI Generated Form');
if (data.description) {
form.setDescription(data.description);
}
form.setIsQuiz(false);
form.setAllowResponseEdits(false);
form.setCollectEmail(false);
data.questions.forEach(function(q) {
var item;
switch (q.type) {
case 'PARAGRAPH':
item = form.addParagraphTextItem();
break;
case 'MULTIPLE_CHOICE':
item = form.addMultipleChoiceItem();
item.setChoiceValues(ensureChoiceOptions(q.options, 'Choice'));
break;
case 'CHECKBOX':
item = form.addCheckboxItem();
item.setChoiceValues(ensureChoiceOptions(q.options, 'Option'));
break;
case 'DROPDOWN':
item = form.addListItem();
item.setChoiceValues(ensureChoiceOptions(q.options, 'Option'));
break;
case 'LINEAR_SCALE':
item = form.addScaleItem();
var bounds = clampScaleBounds(q.scaleMin, q.scaleMax);
item.setBounds(bounds[0], bounds[1]);
// ScaleItem uses setLabels(lower, upper) — not setLeftLabel / setRightLabel
if (q.scaleMinLabel || q.scaleMaxLabel) {
item.setLabels(q.scaleMinLabel || '', q.scaleMaxLabel || '');
}
break;
case 'DATE':
item = form.addDateItem();
break;
case 'TIME':
item = form.addTimeItem();
break;
case 'MULTIPLE_CHOICE_GRID':
item = form.addGridItem();
var gridAxes = ensureGridAxes(q.rows, q.columns);
item.setRows(gridAxes.rows);
item.setColumns(gridAxes.columns);
break;
case 'CHECKBOX_GRID':
item = form.addCheckboxGridItem();
var cbAxes = ensureGridAxes(q.rows, q.columns);
item.setRows(cbAxes.rows);
item.setColumns(cbAxes.columns);
break;
case 'SECTION_HEADER':
item = form.addSectionHeaderItem();
if (q.helpText) item.setHelpText(q.helpText);
break;
default:
item = form.addTextItem();
break;
}
item.setTitle(q.title);
if (q.helpText && q.type !== 'SECTION_HEADER') {
item.setHelpText(q.helpText);
}
if (q.required && typeof item.setRequired === 'function') {
item.setRequired(q.required);
}
});
return {
editUrl: form.getEditUrl(),
publishedUrl: form.getPublishedUrl()
};
}