1 | |
2 |
|
3 | export type DynSelect_sheet_name = string |
4 |
|
5 | |
6 | export async function sheet_name(auth: RT.Gsheets, spreadsheet_id: string) { |
7 | const response = await fetch( |
8 | `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheet_id}?fields=sheets.properties.title`, |
9 | { |
10 | headers: { |
11 | Authorization: `Bearer ${auth.token}`, |
12 | Accept: "application/json", |
13 | }, |
14 | } |
15 | ) |
16 | if (!response.ok) { |
17 | throw new Error(`${response.status} ${await response.text()}`) |
18 | } |
19 | const { sheets } = (await response.json()) as { |
20 | sheets: { properties: { title: string } }[] |
21 | } |
22 | return sheets.map((s) => ({ |
23 | value: s.properties.title, |
24 | label: s.properties.title, |
25 | })) |
26 | } |
27 |
|
28 | |
29 | * Update Row |
30 | * Overwrite one row, by its 1-based row number, starting at column A. Values are parsed as if typed in the UI (formulas, dates, numbers); pass fewer values than columns to leave the rest unchanged. |
31 | */ |
32 | export async function main( |
33 | auth: RT.Gsheets, |
34 | spreadsheet_id: string, |
35 | sheet_name: DynSelect_sheet_name, |
36 | row_number: number, |
37 | values: any[] |
38 | ) { |
39 | const range = encodeURIComponent( |
40 | `'${sheet_name.replaceAll("'", "''")}'!A${row_number}` |
41 | ) |
42 | const url = new URL( |
43 | `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheet_id}/values/${range}` |
44 | ) |
45 | url.searchParams.append("valueInputOption", "USER_ENTERED") |
46 | url.searchParams.append("includeValuesInResponse", "true") |
47 |
|
48 | const response = await fetch(url, { |
49 | method: "PUT", |
50 | headers: { |
51 | Authorization: `Bearer ${auth.token}`, |
52 | "Content-Type": "application/json", |
53 | Accept: "application/json", |
54 | }, |
55 | body: JSON.stringify({ values: [values] }), |
56 | }) |
57 |
|
58 | if (!response.ok) { |
59 | throw new Error(`${response.status} ${await response.text()}`) |
60 | } |
61 |
|
62 | return await response.json() |
63 | } |
64 |
|