0

Create Text Question

by
Published Mar 25, 2024

Creates a new text question in a Google Form. [See the documentation](https://developers.google.com/forms/api/reference/rest/v1/forms/batchUpdate)

Script gforms Verified

The script

Submitted by hugo697 Bun
Verified 398 days ago
1
import google from '@googleapis/forms'
2

3
type Gforms = {
4
	token: string
5
}
6

7
export async function main(
8
	resource: Gforms,
9
	formId: string,
10
	index: number = 0,
11
	title: string,
12
	description: string,
13
	paragraph: boolean = false
14
) {
15
	// setup auth
16
	const auth = new google.auth.OAuth2({})
17
	auth.setCredentials({
18
		access_token: resource.token
19
	})
20

21
	const forms = google.forms({
22
		version: 'v1',
23
		auth: auth
24
	})
25

26
	// create text question
27
	const textQuestion = {
28
		requests: [
29
			{
30
				createItem: {
31
					location: {
32
						index: index
33
					},
34
					item: {
35
						questionItem: {
36
							question: {
37
								textQuestion: {
38
									paragraph: paragraph
39
								}
40
							}
41
						},
42
						title: title,
43
						description: description
44
					}
45
				}
46
			}
47
		]
48
	}
49

50
	const res = await forms.forms.batchUpdate({
51
		formId: formId,
52
		requestBody: textQuestion
53
	})
54

55
	return res.data
56
}
57