1 | |
2 |
|
3 | |
4 | * Insert Image |
5 | * 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. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Gdocs, |
9 | document_id: string, |
10 | image_url: string, |
11 | index: number | undefined, |
12 | width_pt: number | undefined, |
13 | height_pt: number | undefined, |
14 | tab_id: string | undefined |
15 | ) { |
16 | const tabId = tab_id || undefined |
17 | const response = await fetch( |
18 | `https://docs.googleapis.com/v1/documents/${document_id}:batchUpdate`, |
19 | { |
20 | method: "POST", |
21 | headers: { |
22 | Authorization: `Bearer ${auth.token}`, |
23 | "Content-Type": "application/json", |
24 | Accept: "application/json", |
25 | }, |
26 | body: JSON.stringify({ requests: [{ |
27 | insertInlineImage: { |
28 | uri: image_url, |
29 | ...(index == null |
30 | ? { endOfSegmentLocation: { tabId } } |
31 | : { location: { index, tabId } }), |
32 | objectSize: |
33 | width_pt || height_pt |
34 | ? { |
35 | width: width_pt ? { magnitude: width_pt, unit: "PT" } : undefined, |
36 | height: height_pt ? { magnitude: height_pt, unit: "PT" } : undefined, |
37 | } |
38 | : undefined, |
39 | }, |
40 | }] }), |
41 | } |
42 | ) |
43 | if (!response.ok) { |
44 | throw new Error(`${response.status} ${await response.text()}`) |
45 | } |
46 |
|
47 | return await response.json() |
48 | } |
49 |
|