0

List Comments

by
Published today

List the comments and replies on a file.

Script gdrive Verified

The script

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

3
/**
4
 * List Comments
5
 * List the comments on a file (Docs, Sheets, Slides, PDFs, …) with their replies, author, resolved state and quoted text. Set `start_modified_time` (RFC 3339) to only get comments changed since then. Returns `comments` and `nextPageToken`.
6
 */
7
export async function main(
8
  auth: RT.Gdrive,
9
  file_id: string,
10
  include_deleted: boolean = false,
11
  start_modified_time: string | undefined,
12
  page_size: number = 100,
13
  page_token: string | undefined,
14
  fields: string = "nextPageToken, comments(id, content, author(displayName, emailAddress), createdTime, modifiedTime, resolved, deleted, quotedFileContent, replies(id, content, action, author(displayName, emailAddress), createdTime, deleted))"
15
) {
16
  const url = new URL(
17
    `https://www.googleapis.com/drive/v3/files/${file_id}/comments`
18
  )
19
  // comments.list rejects requests without `fields`
20
  url.searchParams.append("fields", fields)
21
  url.searchParams.append("pageSize", String(page_size))
22
  if (include_deleted) url.searchParams.append("includeDeleted", "true")
23
  if (start_modified_time != null && start_modified_time !== "")
24
    url.searchParams.append("startModifiedTime", start_modified_time)
25
  if (page_token != null && page_token !== "")
26
    url.searchParams.append("pageToken", page_token)
27

28
  const response = await fetch(url, {
29
    headers: {
30
      Authorization: `Bearer ${auth.token}`,
31
      Accept: "application/json",
32
    },
33
  })
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41