Edits history of script submission #23312 for ' Create Tab (gdocs)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Tab
     * Add a tab to a Google Doc, optionally nested under `parent_tab_id` (up to 3 levels) and at a 0-based `index` among its siblings. The new tab's id is `replies[0].addDocumentTab.tabProperties.tabId`.
     */
    export async function main(
      auth: RT.Gdocs,
      document_id: string,
      title: string,
      parent_tab_id: string | undefined,
      index: number | undefined
    ) {
      const response = await fetch(
        `https://docs.googleapis.com/v1/documents/${document_id}:batchUpdate`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ requests: [{
              addDocumentTab: {
                tabProperties: {
                  title,
                  parentTabId: parent_tab_id || undefined,
                  index: index ?? undefined,
                },
              },
            }] }),
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago