1 | |
2 |
|
3 | |
4 | * |
5 | * @param startCell Use 'A1' notation. |
6 | * https://developers.google.com/sheets/api/guides/concepts#cell |
7 | * |
8 | * @param responseValueRenderOption Determines how values in the response should be rendered. |
9 | * https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption |
10 | * |
11 | * @param responseDateTimeRenderOption Determines how dates, times, and durations in the response should be rendered. |
12 | * This is ignored if `responseValueRenderOption` is `FORMATTED_VALUE`. |
13 | * https://developers.google.com/sheets/api/reference/rest/v4/DateTimeRenderOption |
14 | */ |
15 | type Gsheets = { |
16 | token: string; |
17 | }; |
18 | export async function main( |
19 | gsheets_auth: Gsheets, |
20 | spreadsheetId: string, |
21 | startCell: string, |
22 | values: Array<Array<any>>, |
23 | majorDimension: "ROWS" | "COLUMNS" = "ROWS", |
24 | valueInputOption: "RAW" | "USER_ENTERED" = "USER_ENTERED", |
25 | responseValueRenderOption: |
26 | | "FORMATTED_VALUE" |
27 | | "UNFORMATTED_VALUE" |
28 | | "FORMULA" = "FORMATTED_VALUE", |
29 | responseDateTimeRenderOption: |
30 | | "SERIAL_NUMBER" |
31 | | "FORMATTED_STRING" = "SERIAL_NUMBER", |
32 | ) { |
33 | const token = gsheets_auth["token"]; |
34 | let UPDATE_VALUES_URL = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${startCell}?includeValuesInResponse=true`; |
35 | UPDATE_VALUES_URL += `&valueInputOption=${valueInputOption}`; |
36 | UPDATE_VALUES_URL += `&responseValueRenderOption=${responseValueRenderOption}`; |
37 | UPDATE_VALUES_URL += `&responseDateTimeRenderOption=${responseDateTimeRenderOption}`; |
38 | const body = { |
39 | values, |
40 | majorDimension, |
41 | }; |
42 |
|
43 | const response = await fetch(UPDATE_VALUES_URL, { |
44 | method: "PUT", |
45 | body: JSON.stringify(body), |
46 | headers: { |
47 | Authorization: "Bearer " + token, |
48 | "Content-Type": "application/json", |
49 | }, |
50 | }); |
51 |
|
52 | const json = await response.json(); |
53 | if (json.error) { |
54 | let { code, status, message } = json.error; |
55 | throw Error(`\n${code} ${status} - "${message}"\n`); |
56 | } |
57 | return json; |
58 | } |
59 |
|