Edits history of script submission #23320 for ' List Tabs (gdocs)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Tabs
     * List a Google Doc's tabs, child tabs included, in document order with their `tab_id`, title, index, nesting level and parent. Tab ids are what the other Docs scripts take as `tab_id`; every document has at least one tab.
     */
    export async function main(auth: RT.Gdocs, document_id: string) {
      const url = new URL(`https://docs.googleapis.com/v1/documents/${document_id}`)
      url.searchParams.append("includeTabsContent", "true")
      // Metadata only: nesting is capped at 3 levels, and childTabs without a mask would pull every tab's body.
      url.searchParams.append(
        "fields",
        "tabs(tabProperties,childTabs(tabProperties,childTabs(tabProperties)))"
      )
      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 { tabs } = (await response.json()) as { tabs?: Tab[] }
      const out: any[] = []
      const walk = (ts: Tab[]) =>
        ts.forEach((t) => {
          const p = t.tabProperties
          out.push({
            tab_id: p.tabId,
            title: p.title,
            index: p.index ?? 0,
            nesting_level: p.nestingLevel ?? 0,
            parent_tab_id: p.parentTabId,
          })
          walk(t.childTabs ?? [])
        })
      walk(tabs ?? [])
      return out
    }
    
    type Tab = {
      tabProperties: {
        tabId: string
        title: string
        index?: number
        nestingLevel?: number
        parentTabId?: string
      }
      childTabs?: Tab[]
    }
    

    Submitted by hugo989 6 hours ago