Modify records of a table
One script reply has been approved by the moderators Verified
Created by hugo697 329 days ago Picked 1 time
Submitted by hugo697 Bun
Verified 329 days ago
1
//native
2
type Grist = {
3
  apiKey: string;
4
  host: string;
5
};
6
/**
7
 * Modify records of a table
8
 *
9
 */
10
export async function main(
11
  auth: Grist,
12
  docId: string,
13
  tableId: string,
14
  noparse: string | undefined,
15
  body: { records: { id: number; fields: {} }[] },
16
) {
17
  const url = new URL(
18
    `https://${auth.host}/api/docs/${docId}/tables/${tableId}/records`,
19
  );
20
  for (const [k, v] of [["noparse", noparse]]) {
21
    if (v !== undefined && v !== "" && k !== undefined) {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "PATCH",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.apiKey,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.text();
38
}
39