1 | import * as wmill from "https://deno.land/x/windmill@v1.46.2/mod.ts"; |
2 |
|
3 |
|
4 | * If the `endIndex` argument is not specified, only one row will be deleted. |
5 | * |
6 | * **Indexes start from 1 and both are inclusive.** |
7 | * |
8 | * *(eg: if `startIndex = 1` and `endIndex = 3` then row 1, 2 and 3 will be deleted)* */ |
9 | export async function main( |
10 | gsheets_auth: wmill.Resource<"gsheets">, |
11 | spreadsheetId: string, |
12 | sheetId: string, |
13 | startIndex: number, |
14 | endIndex?: number |
15 | ) { |
16 | const token = gsheets_auth["token"]; |
17 | const DELETE_ROWS_URL = |
18 | `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}:batchUpdate`; |
19 |
|
20 | startIndex = startIndex - 1 |
21 | endIndex = endIndex ?? startIndex + 1 |
22 | const body = { |
23 | requests: [ |
24 | { |
25 | deleteDimension: { |
26 | range: { |
27 | dimension: "ROWS", |
28 | sheetId, |
29 | startIndex, |
30 | endIndex |
31 | } |
32 | } |
33 | } |
34 | ] |
35 | } |
36 |
|
37 | const response = await fetch(DELETE_ROWS_URL, { |
38 | method: "POST", |
39 | body: JSON.stringify(body), |
40 | headers: { |
41 | Authorization: "Bearer " + token, |
42 | "Content-Type": "application/json", |
43 | }, |
44 | }); |
45 |
|
46 | const json = await response.json() |
47 | if(json.error) { |
48 | let { code, status, message } = json.error |
49 | message = message.replaceAll('indexes must be >= 0', 'indexes must be > 0') |
50 | throw Error(`\n${code} ${status} - "${message}"\n`) |
51 | } |
52 | if(endIndex - startIndex === 1) { |
53 | return `Deleted row: ${endIndex}`; |
54 | } |
55 | return `Deleted rows: ${startIndex + 1}-${endIndex}`; |
56 | } |