0

Add Summary Fields

Published Oct 17, 2025

Creates one or more summary fields for the specified sheet.

Script smartsheet Verified

The script

Submitted by hugo697 Bun
Verified 397 days ago
1
//native
2
type Smartsheet = {
3
  token: string;
4
  baseUrl: string;
5
};
6
/**
7
 * Add Summary Fields
8
 * Creates one or more summary fields for the specified sheet.
9
 */
10
export async function main(
11
  auth: Smartsheet,
12
  sheetId: string,
13
  renameIfConflict: string | undefined,
14
  body: {
15
    contactOptions?: { email?: string; name?: string }[];
16
    format?: string;
17
    formula?: string;
18
    hyperlink?: {
19
      reportId?: number;
20
      sheetId?: number;
21
      sightId?: number;
22
      url?: string;
23
    };
24
    image?: { altText?: string; height?: number; id?: string; width?: number };
25
    index?: number;
26
    locked?: false | true;
27
    objectValue?:
28
      | { objectType?: "ABSTRACT_DATETIME"; value?: string }
29
      | { objectType?: "CHECKBOX"; value?: false | true }
30
      | {
31
          objectType?: "CONTACT";
32
          email?: string;
33
          name?: string;
34
          imageId?: string;
35
        }
36
      | { objectType?: "DATE"; value?: string }
37
      | { objectType?: "DATETIME"; value?: string }
38
      | { objectType?: "DURATION"; days?: number }
39
      | {
40
          objectType?: "MULTI_CONTACT";
41
          value?: {
42
            objectType?: "CONTACT";
43
            email?: string;
44
            name?: string;
45
            imageId?: string;
46
          }[];
47
        }
48
      | { objectType?: "MULTI_PICKLIST"; value?: string[] }
49
      | {
50
          objectType?: "PREDECESSOR_LIST";
51
          predecessors?: {
52
            rowId?: number;
53
            type?: "FF" | "FS" | "SF" | "SS";
54
            inCriticalPath?: false | true;
55
            invalid?: false | true;
56
            lag?: {
57
              days?: number;
58
              elapsed?: false | true;
59
              hours?: number;
60
              milliseconds?: number;
61
              minutes?: number;
62
              negative?: false | true;
63
              objectType?: "DURATION";
64
              seconds?: number;
65
              weeks?: number;
66
            };
67
            rowNumber?: number;
68
          }[];
69
        };
70
    options?: string[];
71
    symbol?: string;
72
    title?: string;
73
    type?:
74
      | "ABSTRACT_DATETIME"
75
      | "CHECKBOX"
76
      | "DATE"
77
      | "DATETIME"
78
      | "DURATION"
79
      | "MULTI_PICKLIST"
80
      | "CONTACT_LIST"
81
      | "MULTI_CONTACT_LIST"
82
      | "PICKLIST"
83
      | "PREDECESSOR"
84
      | "TEXT_NUMBER";
85
    validation?: false | true;
86
  }[],
87
) {
88
  const url = new URL(
89
    `${auth.baseUrl}/sheets/${sheetId}/summary/fields`,
90
  );
91
  for (const [k, v] of [["renameIfConflict", renameIfConflict]]) {
92
    if (v !== undefined && v !== "" && k !== undefined) {
93
      url.searchParams.append(k, v);
94
    }
95
  }
96
  const response = await fetch(url, {
97
    method: "POST",
98
    headers: {
99
      "Content-Type": "application/json",
100
      Authorization: "Bearer " + auth.token,
101
    },
102
    body: JSON.stringify(body),
103
  });
104
  if (!response.ok) {
105
    const text = await response.text();
106
    throw new Error(`${response.status} ${text}`);
107
  }
108
  return await response.json();
109
}
110