0

Attach File or URL to Comment

by
Published Oct 17, 2025

Attaches a file to the comment.

Script smartsheet Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Smartsheet = {
3
  token: string;
4
  baseUrl: string;
5
};
6
/**
7
 * Attach File or URL to Comment
8
 * Attaches a file to the comment.
9
 */
10
export async function main(
11
  auth: Smartsheet,
12
  sheetId: string,
13
  commentId: string,
14
  body: {
15
    attachmentSubType?:
16
      | "DOCUMENT"
17
      | "DRAWING"
18
      | "FOLDER"
19
      | "PDF"
20
      | "PRESENTATION"
21
      | "SPREADSHEET";
22
    attachmentType?:
23
      | "BOX_COM"
24
      | "DROPBOX"
25
      | "EGNYTE"
26
      | "EVERNOTE"
27
      | "FILE"
28
      | "GOOGLE_DRIVE"
29
      | "LINK"
30
      | "ONEDRIVE"
31
      | "TRELLO";
32
    description?: string;
33
    name?: string;
34
    url?: string;
35
  },
36
) {
37
  const url = new URL(
38
    `${auth.baseUrl}/sheets/${sheetId}/comments/${commentId}/attachments`,
39
  );
40

41
  const formData = new FormData();
42
  for (const [k, v] of Object.entries(body)) {
43
    if (v !== undefined && v !== "") {
44
      formData.append(k, String(v));
45
    }
46
  }
47
  const response = await fetch(url, {
48
    method: "POST",
49
    headers: {
50
      "Content-Type": "application/json",
51
      Authorization: "Bearer " + auth.token,
52
    },
53
    body: JSON.stringify(body),
54
  });
55
  if (!response.ok) {
56
    const text = await response.text();
57
    throw new Error(`${response.status} ${text}`);
58
  }
59
  return await response.json();
60
}
61