//native
/**
* Insert Image
* Insert an inline image into a Google Doc from a publicly reachable PNG, JPEG or GIF URL (max 50 MB / 25 megapixels). Leave `index` empty to append at the end. Optional `width_pt`/`height_pt` scale it (aspect ratio is kept). The reply's `replies[0].insertInlineImage.objectId` is the id Replace Image takes. Set `tab_id` (from List Tabs) to target a tab other than the first.
*/
export async function main(
auth: RT.Gdocs,
document_id: string,
image_url: string,
index: number | undefined,
width_pt: number | undefined,
height_pt: number | undefined,
tab_id: string | undefined
) {
const tabId = tab_id || 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: [{
insertInlineImage: {
uri: image_url,
...(index == null
? { endOfSegmentLocation: { tabId } }
: { location: { index, tabId } }),
objectSize:
width_pt || height_pt
? {
width: width_pt ? { magnitude: width_pt, unit: "PT" } : undefined,
height: height_pt ? { magnitude: height_pt, unit: "PT" } : undefined,
}
: undefined,
},
}] }),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 6 hours ago