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