0

List Comments

by
Published today

List the comments and replies on a Google Doc. Requires drive.readonly or drive on the connection.

Script gdocs Verified

The script

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

3
/**
4
 * List Comments
5
 * List the comments on a Google Doc (or any Drive file), each with its author, content, quoted text, `resolved` state and replies. Pass the returned `nextPageToken` as `page_token` for the next page. Requires drive.readonly or drive on the connection (the documents scope doesn't cover the Drive comments API).
6
 */
7
export async function main(
8
  auth: RT.Gdocs,
9
  document_id: string,
10
  include_deleted: boolean = false,
11
  page_size: number = 100,
12
  page_token: string | undefined
13
) {
14
  const url = new URL(
15
    `https://www.googleapis.com/drive/v3/files/${document_id}/comments`
16
  )
17
  // The comments API rejects requests without a field mask.
18
  url.searchParams.append("fields", "comments,nextPageToken")
19
  url.searchParams.append("includeDeleted", String(!!include_deleted))
20
  url.searchParams.append("pageSize", String(page_size || 100))
21
  if (page_token != null && page_token !== "")
22
    url.searchParams.append("pageToken", page_token)
23

24
  const response = await fetch(url, {
25
    headers: {
26
      Authorization: `Bearer ${auth.token}`,
27
      Accept: "application/json",
28
    },
29
  })
30
  if (!response.ok) {
31
    throw new Error(`${response.status} ${await response.text()}`)
32
  }
33

34
  return await response.json()
35
}
36