Edits history of script submission #23244 for ' List Spreadsheets (gsheets)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Spreadsheets
     * List the spreadsheets the connected account can open, most recently modified first, optionally filtered by name. Uses the Drive API, so the connection needs drive.metadata.readonly (or drive.readonly / drive) on top of the spreadsheets scope. Returns `files` and `nextPageToken`.
     */
    export async function main(
      auth: RT.Gsheets,
      name_contains: string | undefined,
      page_size: number = 100,
      page_token: string | undefined
    ) {
      let q =
        "mimeType = 'application/vnd.google-apps.spreadsheet' and trashed = false"
      if (name_contains != null && name_contains !== "")
        q += ` and name contains '${name_contains.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}'`
    
      const url = new URL("https://www.googleapis.com/drive/v3/files")
      url.searchParams.append("q", q)
      url.searchParams.append("orderBy", "modifiedTime desc")
      url.searchParams.append("pageSize", String(page_size))
      url.searchParams.append(
        "fields",
        "nextPageToken, files(id, name, modifiedTime, webViewLink, owners(displayName, emailAddress))"
      )
      url.searchParams.append("supportsAllDrives", "true")
      url.searchParams.append("includeItemsFromAllDrives", "true")
      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