0

Create Tab

by
Published today

Add a tab (or a child tab) to a Google Doc.

Script gdocs Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
/**
4
 * Create Tab
5
 * Add a tab to a Google Doc, optionally nested under `parent_tab_id` (up to 3 levels) and at a 0-based `index` among its siblings. The new tab's id is `replies[0].addDocumentTab.tabProperties.tabId`.
6
 */
7
export async function main(
8
  auth: RT.Gdocs,
9
  document_id: string,
10
  title: string,
11
  parent_tab_id: string | undefined,
12
  index: number | undefined
13
) {
14
  const response = await fetch(
15
    `https://docs.googleapis.com/v1/documents/${document_id}:batchUpdate`,
16
    {
17
      method: "POST",
18
      headers: {
19
        Authorization: `Bearer ${auth.token}`,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({ requests: [{
24
          addDocumentTab: {
25
            tabProperties: {
26
              title,
27
              parentTabId: parent_tab_id || undefined,
28
              index: index ?? undefined,
29
            },
30
          },
31
        }] }),
32
    }
33
  )
34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40