import * as wmill from "https://deno.land/x/windmill@v1.46.2/mod.ts";
/** Delete row(s) in the given range.
* If the `endIndex` argument is not specified, only one row will be deleted.
*
* **Indexes start from 1 and both are inclusive.**
*
* *(eg: if `startIndex = 1` and `endIndex = 3` then row 1, 2 and 3 will be deleted)* */
export async function main(
gsheets_auth: wmill.Resource<"gsheets">,
spreadsheetId: string,
sheetId: string,
startIndex: number,
endIndex?: number
) {
const token = gsheets_auth["token"];
const DELETE_ROWS_URL =
`https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}:batchUpdate`;
startIndex = startIndex - 1
endIndex = endIndex ?? startIndex + 1
const body = {
requests: [
{
deleteDimension: {
range: {
dimension: "ROWS",
sheetId,
startIndex,
endIndex
}
}
}
]
}
const response = await fetch(DELETE_ROWS_URL, {
method: "POST",
body: JSON.stringify(body),
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
});
const json = await response.json()
if(json.error) {
let { code, status, message } = json.error
message = message.replaceAll('indexes must be >= 0', 'indexes must be > 0')
throw Error(`\n${code} ${status} - "${message}"\n`)
}
if(endIndex - startIndex === 1) {
return `Deleted row: ${endIndex}`;
}
return `Deleted rows: ${startIndex + 1}-${endIndex}`;
}
Submitted by adam-kov 739 days ago