Edits history of script submission #23021 for ' Get Document as Text (gdocs)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Document as Text
     * Get a Google Doc's title and its body as plain text (paragraphs and table cells, in reading order). Reads the first tab unless `tab_id` (from List Tabs) names another one.
     */
    export async function main(
      auth: RT.Gdocs,
      document_id: string,
      tab_id: string | undefined
    ) {
      const url = new URL(`https://docs.googleapis.com/v1/documents/${document_id}`)
      if (tab_id != null && tab_id !== "")
        url.searchParams.append("includeTabsContent", "true")
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const doc = await response.json()
      let content = doc.body?.content
      if (tab_id) {
        const tab = findTab(doc.tabs ?? [], tab_id)
        if (!tab) throw new Error(`No tab ${tab_id} in document ${document_id}`)
        content = tab.documentTab?.body?.content
      }
      return {
        document_id: doc.documentId,
        title: doc.title,
        text: toText(content ?? []),
      }
    }
    
    function findTab(tabs: any[], id: string): any {
      for (const t of tabs) {
        if (t.tabProperties?.tabId === id) return t
        const child = findTab(t.childTabs ?? [], id)
        if (child) return child
      }
    }
    
    function toText(content: any[]): string {
      let text = ""
      for (const el of content) {
        for (const e of el.paragraph?.elements ?? [])
          text += e.textRun?.content ?? ""
        for (const row of el.table?.tableRows ?? [])
          for (const cell of row.tableCells ?? [])
            text += toText(cell.content ?? [])
        if (el.tableOfContents) text += toText(el.tableOfContents.content ?? [])
      }
      return text
    }
    

    Submitted by hugo989 8 days ago

  • bunnative
    //native
    
    /**
     * Get Document as Text
     * Get a Google Doc's title and its body as plain text (paragraphs and table cells, in reading order). Only the first tab of a multi-tab document is read.
     */
    export async function main(auth: RT.Gdocs, document_id: string) {
      const response = await fetch(
        `https://docs.googleapis.com/v1/documents/${document_id}`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const doc = await response.json()
      return {
        document_id: doc.documentId,
        title: doc.title,
        text: toText(doc.body?.content ?? []),
      }
    }
    
    function toText(content: any[]): string {
      let text = ""
      for (const el of content) {
        for (const e of el.paragraph?.elements ?? [])
          text += e.textRun?.content ?? ""
        for (const row of el.table?.tableRows ?? [])
          for (const cell of row.tableCells ?? [])
            text += toText(cell.content ?? [])
        if (el.tableOfContents) text += toText(el.tableOfContents.content ?? [])
      }
      return text
    }
    

    Submitted by hugo989 21 days ago