Edits history of script submission #23185 for ' List Comments (gdrive)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Comments
     * 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`.
     */
    export async function main(
      auth: RT.Gdrive,
      file_id: string,
      include_deleted: boolean = false,
      start_modified_time: string | undefined,
      page_size: number = 100,
      page_token: string | undefined,
      fields: string = "nextPageToken, comments(id, content, author(displayName, emailAddress), createdTime, modifiedTime, resolved, deleted, quotedFileContent, replies(id, content, action, author(displayName, emailAddress), createdTime, deleted))"
    ) {
      const url = new URL(
        `https://www.googleapis.com/drive/v3/files/${file_id}/comments`
      )
      // comments.list rejects requests without `fields`
      url.searchParams.append("fields", fields)
      url.searchParams.append("pageSize", String(page_size))
      if (include_deleted) url.searchParams.append("includeDeleted", "true")
      if (start_modified_time != null && start_modified_time !== "")
        url.searchParams.append("startModifiedTime", start_modified_time)
      if (page_token != null && page_token !== "")
        url.searchParams.append("pageToken", page_token)
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago