1 | |
2 | type Grist = { |
3 | apiKey: string; |
4 | host: string; |
5 | }; |
6 | |
7 | * Add or update columns of a table |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Grist, |
12 | docId: string, |
13 | tableId: string, |
14 | noadd: string | undefined, |
15 | noupdate: string | undefined, |
16 | replaceall: string | undefined, |
17 | body: { |
18 | columns: { |
19 | id: string; |
20 | fields: { |
21 | type?: |
22 | | "Any" |
23 | | "Text" |
24 | | "Numeric" |
25 | | "Int" |
26 | | "Bool" |
27 | | "Date" |
28 | | "DateTime:<timezone>" |
29 | | "Choice" |
30 | | "ChoiceList" |
31 | | "Ref:<tableId>" |
32 | | "RefList:<tableId>" |
33 | | "Attachments"; |
34 | label?: string; |
35 | formula?: string; |
36 | isFormula?: false | true; |
37 | widgetOptions?: string; |
38 | untieColIdFromLabel?: false | true; |
39 | recalcWhen?: number; |
40 | visibleCol?: number; |
41 | } & { recalcDeps?: string } & { colId?: string }; |
42 | }[]; |
43 | }, |
44 | ) { |
45 | const url = new URL( |
46 | `https://${auth.host}/api/docs/${docId}/tables/${tableId}/columns`, |
47 | ); |
48 | for (const [k, v] of [ |
49 | ["noadd", noadd], |
50 | ["noupdate", noupdate], |
51 | ["replaceall", replaceall], |
52 | ]) { |
53 | if (v !== undefined && v !== "" && k !== undefined) { |
54 | url.searchParams.append(k, v); |
55 | } |
56 | } |
57 | const response = await fetch(url, { |
58 | method: "PUT", |
59 | headers: { |
60 | "Content-Type": "application/json", |
61 | Authorization: "Bearer " + auth.apiKey, |
62 | }, |
63 | body: JSON.stringify(body), |
64 | }); |
65 | if (!response.ok) { |
66 | const text = await response.text(); |
67 | throw new Error(`${response.status} ${text}`); |
68 | } |
69 | return await response.text(); |
70 | } |
71 |
|