Edits history of script submission #23321 for ' Replace Image (gdocs)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Replace Image
     * Replace an existing inline image in a Google Doc with one from a publicly reachable URL, keeping its position. `image_object_id` is a key of the document's `inlineObjects` (Get Document) or the `objectId` returned by Insert Image. Set `tab_id` when the image isn't in the first tab.
     */
    export async function main(
      auth: RT.Gdocs,
      document_id: string,
      image_object_id: string,
      image_url: string,
      tab_id: string | 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: [{
              replaceImage: {
                imageObjectId: image_object_id,
                uri: image_url,
                imageReplaceMethod: "CENTER_CROP",
                tabId: tab_id || undefined,
              },
            }] }),
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago