0

Insert Page Break

by
Published today

Insert a page break into a Google Doc.

Script gdocs Verified

The script

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

3
/**
4
 * Insert Page Break
5
 * Insert a page break into a Google Doc's body at a character index, or at the end when `index` is empty. Set `tab_id` (from List Tabs) to target a tab other than the first.
6
 */
7
export async function main(
8
  auth: RT.Gdocs,
9
  document_id: string,
10
  index: number | undefined,
11
  tab_id: string | undefined
12
) {
13
  const tabId = tab_id || undefined
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
          insertPageBreak:
25
            index == null
26
              ? { endOfSegmentLocation: { tabId } }
27
              : { location: { index, tabId } },
28
        }] }),
29
    }
30
  )
31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`)
33
  }
34

35
  return await response.json()
36
}
37