0

Modify columns of a table

by
Published Apr 8, 2025
Script grist Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Grist = {
3
  apiKey: string;
4
  host: string;
5
};
6
/**
7
 * Modify columns of a table
8
 *
9
 */
10
export async function main(
11
  auth: Grist,
12
  docId: string,
13
  tableId: string,
14
  body: {
15
    columns: {
16
      id: string;
17
      fields: {
18
        type?:
19
          | "Any"
20
          | "Text"
21
          | "Numeric"
22
          | "Int"
23
          | "Bool"
24
          | "Date"
25
          | "DateTime:<timezone>"
26
          | "Choice"
27
          | "ChoiceList"
28
          | "Ref:<tableId>"
29
          | "RefList:<tableId>"
30
          | "Attachments";
31
        label?: string;
32
        formula?: string;
33
        isFormula?: false | true;
34
        widgetOptions?: string;
35
        untieColIdFromLabel?: false | true;
36
        recalcWhen?: number;
37
        visibleCol?: number;
38
      } & { recalcDeps?: string } & { colId?: string };
39
    }[];
40
  },
41
) {
42
  const url = new URL(
43
    `https://${auth.host}/api/docs/${docId}/tables/${tableId}/columns`,
44
  );
45

46
  const response = await fetch(url, {
47
    method: "PATCH",
48
    headers: {
49
      "Content-Type": "application/json",
50
      Authorization: "Bearer " + auth.apiKey,
51
    },
52
    body: JSON.stringify(body),
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.text();
59
}
60