Edits history of script submission #23022 for ' Insert Text (gdocs)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Insert Text
     * Insert text into a Google Doc at a character index (1 is the start of the body). Leave `index` empty to append at the end of the document.
     */
    export async function main(
      auth: RT.Gdocs,
      document_id: string,
      text: string,
      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: [
              {
                insertText:
                  index === undefined
                    ? { text, endOfSegmentLocation: {} }
                    : { text, location: { index } },
              },
            ],
          }),
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago