Content of document, as an Excel file
One script reply has been approved by the moderators Verified
Created by hugo697 329 days ago Picked 2 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 document, as an Excel file
8
 *
9
 */
10
export async function main(
11
  auth: Grist,
12
  docId: string,
13
  header: "colId" | "label" | undefined,
14
) {
15
  const url = new URL(`https://${auth.host}/api/docs/${docId}/download/xlsx`);
16
  for (const [k, v] of [["header", header]]) {
17
    if (v !== undefined && v !== "" && k !== undefined) {
18
      url.searchParams.append(k, v);
19
    }
20
  }
21
  const response = await fetch(url, {
22
    method: "GET",
23
    headers: {
24
      Authorization: "Bearer " + auth.apiKey,
25
    },
26
    body: undefined,
27
  });
28
  if (!response.ok) {
29
    const text = await response.text();
30
    throw new Error(`${response.status} ${text}`);
31
  }
32
  return await response.text();
33
}
34