• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #66

26 Jun 2026 09:41AM UTC coverage: 59.997% (-0.006%) from 60.003%
#66

push

github

web-flow
Prepare for release v4.1.0 (#180)

4510 of 7517 relevant lines covered (60.0%)

0.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.29
/src/main/java/com/smartsheet/api/internal/SheetResourcesImpl.java
1
/*
2
 * Copyright (C) 2025 Smartsheet
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.smartsheet.api.internal;
18

19
import com.smartsheet.api.AuthorizationException;
20
import com.smartsheet.api.InvalidRequestException;
21
import com.smartsheet.api.ResourceNotFoundException;
22
import com.smartsheet.api.ServiceUnavailableException;
23
import com.smartsheet.api.SheetAttachmentResources;
24
import com.smartsheet.api.SheetAutomationRuleResources;
25
import com.smartsheet.api.SheetColumnResources;
26
import com.smartsheet.api.SheetCommentResources;
27
import com.smartsheet.api.SheetCrossSheetReferenceResources;
28
import com.smartsheet.api.SheetDiscussionResources;
29
import com.smartsheet.api.SheetFilterResources;
30
import com.smartsheet.api.SheetResources;
31
import com.smartsheet.api.SheetRowResources;
32
import com.smartsheet.api.SheetSummaryResources;
33
import com.smartsheet.api.SheetUpdateRequestResources;
34
import com.smartsheet.api.SmartsheetException;
35
import com.smartsheet.api.SmartsheetRestException;
36
import com.smartsheet.api.UserResources;
37
import com.smartsheet.api.internal.http.HttpEntity;
38
import com.smartsheet.api.internal.http.HttpMethod;
39
import com.smartsheet.api.internal.http.HttpRequest;
40
import com.smartsheet.api.internal.http.HttpResponse;
41
import com.smartsheet.api.internal.util.QueryUtil;
42
import com.smartsheet.api.internal.util.Util;
43
import com.smartsheet.api.models.ContainerDestination;
44
import com.smartsheet.api.models.MultiRowEmail;
45
import com.smartsheet.api.models.PagedResult;
46
import com.smartsheet.api.models.PaginationParameters;
47
import com.smartsheet.api.models.Sheet;
48
import com.smartsheet.api.models.SheetEmail;
49
import com.smartsheet.api.models.SheetPublish;
50
import com.smartsheet.api.models.SortSpecifier;
51
import com.smartsheet.api.models.UpdateRequest;
52
import com.smartsheet.api.models.SheetPathNode;
53
import com.smartsheet.api.models.enums.CopyExclusion;
54
import com.smartsheet.api.models.enums.ObjectExclusion;
55
import com.smartsheet.api.models.enums.PaperSize;
56
import com.smartsheet.api.models.enums.SheetCopyInclusion;
57
import com.smartsheet.api.models.enums.SheetInclusion;
58
import com.smartsheet.api.models.enums.SheetTemplateInclusion;
59
import com.smartsheet.api.models.enums.SourceInclusion;
60

61
import java.io.ByteArrayInputStream;
62
import java.io.ByteArrayOutputStream;
63
import java.io.File;
64
import java.io.FileInputStream;
65
import java.io.FileNotFoundException;
66
import java.io.IOException;
67
import java.io.InputStream;
68
import java.io.OutputStream;
69
import java.text.SimpleDateFormat;
70
import java.util.Date;
71
import java.util.EnumSet;
72
import java.util.HashMap;
73
import java.util.Map;
74
import java.util.Set;
75

76
/**
77
 * This is the implementation of the SheetResources.
78
 * <p>
79
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
80
 */
81
public class SheetResourcesImpl extends AbstractResources implements SheetResources {
82

83
    /**
84
     * The Constant BUFFER_SIZE.
85
     */
86
    private static final int BUFFER_SIZE = 4098;
87

88
    private static final String SHEETS = "sheets";
89
    private static final String TEXT_CSV = "text/csv";
90
    private static final String FOLDERS = "folders";
91
    private static final String INCLUDE = "include";
92
    private static final String IMPORT = "import";
93
    private static final String WORKSPACES = "workspaces";
94
    private static final String XLSX_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
95

96
    /**
97
     * Represents the SheetRowResources.
98
     * <p>
99
     * It will be initialized in constructor and will not change afterward.
100
     */
101
    private SheetRowResources rows;
102
    /**
103
     * Represents the SheetColumnResources.
104
     * <p>
105
     * It will be initialized in constructor and will not change afterward.
106
     */
107
    private SheetColumnResources columns;
108
    /**
109
     * Represents the AssociatedAttachmentResources.
110
     * <p>
111
     * It will be initialized in constructor and will not change afterward.
112
     */
113
    private SheetAttachmentResources attachments;
114
    /**
115
     * Represents the AssociatedDiscussionResources.
116
     * <p>
117
     * It will be initialized in constructor and will not change afterward.
118
     */
119
    private SheetDiscussionResources discussions;
120

121
    /**
122
     * Represents the SheetCommentResources.
123
     * <p>
124
     * It will be initialized in constructor and will not change afterward
125
     */
126
    private SheetCommentResources comments;
127

128
    /**
129
     * Represents the SheetUpdateRequestResources.
130
     * <p>
131
     * It will be initialized in constructor and will not change afterward
132
     */
133
    private SheetUpdateRequestResources updateRequests;
134

135
    /**
136
     * Represents the SheetFilterResources.
137
     * <p>
138
     * It will be initialized in constructor and will not change afterward
139
     */
140
    private SheetFilterResources filters;
141

142
    /**
143
     * Represents the AutomationRules.
144
     * <p>
145
     * It will be initialized in the constructor and will not change afterward
146
     */
147
    private SheetAutomationRuleResources automationRules;
148

149
    /**
150
     * Represents the CrossSheetReferences
151
     * <p>
152
     * It will be initialized in the constructor and will not change afterward
153
     */
154
    private SheetCrossSheetReferenceResources crossSheetReferences;
155

156
    /**
157
     * Represents the sheetSummary
158
     * <p>
159
     * It will be initialized in the constructor and will not change afterward
160
     */
161
    private SheetSummaryResources sheetSummary;
162

163
    /**
164
     * Constructor.
165
     *
166
     * @param smartsheet the smartsheet
167
     * @throws IllegalArgumentException : if any argument is null
168
     */
169
    public SheetResourcesImpl(SmartsheetImpl smartsheet) {
170
        super(smartsheet);
1✔
171
        this.rows = new SheetRowResourcesImpl(smartsheet);
1✔
172
        this.columns = new SheetColumnResourcesImpl(smartsheet);
1✔
173
        this.attachments = new SheetAttachmentResourcesImpl(smartsheet);
1✔
174
        this.discussions = new SheetDiscussionResourcesImpl(smartsheet);
1✔
175
        this.comments = new SheetCommentResourcesImpl(smartsheet);
1✔
176
        this.updateRequests = new SheetUpdateRequestResourcesImpl(smartsheet);
1✔
177
        this.filters = new SheetFilterResourcesImpl(smartsheet);
1✔
178
        this.automationRules = new SheetAutomationRuleResourcesImpl(smartsheet);
1✔
179
        this.crossSheetReferences = new SheetCrossSheetReferenceResourcesImpl(smartsheet);
1✔
180
        this.sheetSummary = new SheetSummaryResourcesImpl(smartsheet);
1✔
181
    }
1✔
182

183
    /**
184
     * List all sheets.
185
     * <p>
186
     * It mirrors to the following Smartsheet REST API method: GET /sheets
187
     *
188
     * @return A list of all sheets (note that an empty list will be returned if there are none).
189
     * @throws IllegalArgumentException    if any argument is null or empty string
190
     * @throws InvalidRequestException     if there is any problem with the REST API request
191
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
192
     * @throws ResourceNotFoundException   if the resource cannot be found
193
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
194
     * @throws SmartsheetException         if there is any other error during the operation
195
     */
196
    public PagedResult<Sheet> listSheets() throws SmartsheetException {
197
        return this.listSheets(null, null, null);
×
198
    }
199

200
    /**
201
     * List all sheets.
202
     * <p>
203
     * It mirrors to the following Smartsheet REST API method: GET /sheets
204
     *
205
     * @param includes   the source inclusion
206
     * @param pagination the object containing the pagination parameters
207
     * @return A list of all sheets (note that an empty list will be returned if there are none).
208
     * @throws IllegalArgumentException    if any argument is null or empty string
209
     * @throws InvalidRequestException     if there is any problem with the REST API request
210
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
211
     * @throws ResourceNotFoundException   if the resource cannot be found
212
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
213
     * @throws SmartsheetException         if there is any other error during the operation
214
     */
215
    public PagedResult<Sheet> listSheets(EnumSet<SourceInclusion> includes, PaginationParameters pagination) throws SmartsheetException {
216
        return this.listSheets(includes, pagination, null);
×
217
    }
218

219
    /**
220
     * List Sheets
221
     */
222
    public PagedResult<Sheet> listSheets(
223
            EnumSet<SourceInclusion> includes,
224
            PaginationParameters pagination,
225
            Date modifiedSince
226
    ) throws SmartsheetException {
227
        String path = SHEETS;
1✔
228

229
        Map<String, Object> parameters = new HashMap<>();
1✔
230
        if (pagination != null) {
1✔
231
            parameters = pagination.toHashMap();
1✔
232
        }
233
        if (modifiedSince != null) {
1✔
234
            String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(modifiedSince);
×
235
            parameters.put("modifiedSince", isoDate);
×
236
        }
237
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
238

239
        path += QueryUtil.generateUrl(null, parameters);
1✔
240
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
241
    }
242

243
    /**
244
     * List all sheets in the organization.
245
     * <p>
246
     * It mirrors to the following Smartsheet REST API method: GET /users/sheets
247
     *
248
     * @param parameters the object containing the pagination parameters
249
     * @return all sheets (note that empty list will be returned if there is none)
250
     * @throws InvalidRequestException     : if there is any problem with the REST API request
251
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
252
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
253
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
254
     * @throws SmartsheetException         : if there is any other error occurred during the operation
255
     * @deprecated As of release 2.0. Please use {@link UserResources} instead
256
     */
257
    @Deprecated(since = "2.0.0", forRemoval = true)
258
    public PagedResult<Sheet> listOrganizationSheets(PaginationParameters parameters) throws SmartsheetException {
259
        String path = "users/sheets";
1✔
260

261
        if (parameters != null) {
1✔
262
            path += parameters.toQueryString();
1✔
263
        }
264
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
265
    }
266

267
    /**
268
     * Get a sheet.
269
     * <p>
270
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
271
     *
272
     * @param id         the id
273
     * @param includes   used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
274
     * @param columnIds  the column ids
275
     * @param excludes   the exclude parameters
276
     * @param page       the page number
277
     * @param pageSize   the page size
278
     * @param rowIds     the row ids
279
     * @param rowNumbers the row numbers
280
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
281
     * rather than returning null).
282
     * @throws InvalidRequestException     : if there is any problem with the REST API request
283
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
284
     * @throws ResourceNotFoundException   : if the resource can not be found
285
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
286
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
287
     * @throws SmartsheetException         : if there is any other error occurred during the operation
288
     */
289
    public Sheet getSheet(
290
            long id,
291
            EnumSet<SheetInclusion> includes,
292
            EnumSet<ObjectExclusion> excludes,
293
            Set<Long> rowIds,
294
            Set<Integer> rowNumbers,
295
            Set<Long> columnIds,
296
            Integer pageSize,
297
            Integer page
298
    ) throws SmartsheetException {
299
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, null, null);
1✔
300
    }
301

302
    /**
303
     * Get a sheet.
304
     * <p>
305
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
306
     *
307
     * @param id             the id
308
     * @param includes       used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
309
     * @param columnIds      the column ids
310
     * @param excludes       the exclude parameters
311
     * @param page           the page number
312
     * @param pageSize       the page size
313
     * @param rowIds         the row ids
314
     * @param rowNumbers     the row numbers
315
     * @param ifVersionAfter only fetch Sheet if more recent version available
316
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
317
     * rather than returning null).
318
     * @throws InvalidRequestException     : if there is any problem with the REST API request
319
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
320
     * @throws ResourceNotFoundException   : if the resource can not be found
321
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
322
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
323
     * @throws SmartsheetException         : if there is any other error occurred during the operation
324
     */
325
    public Sheet getSheet(
326
            long id,
327
            EnumSet<SheetInclusion> includes,
328
            EnumSet<ObjectExclusion> excludes,
329
            Set<Long> rowIds,
330
            Set<Integer> rowNumbers,
331
            Set<Long> columnIds,
332
            Integer pageSize,
333
            Integer page,
334
            Integer ifVersionAfter
335
    ) throws SmartsheetException {
336
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, ifVersionAfter, null);
×
337
    }
338

339
    /**
340
     * Get a sheet.
341
     * <p>
342
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
343
     *
344
     * @param id             the id of the sheet
345
     * @param includes       used to specify the optional objects to include.
346
     * @param columnIds      the column ids
347
     * @param excludes       the exclude parameters
348
     * @param page           the page number
349
     * @param pageSize       the page size
350
     * @param rowIds         the row ids
351
     * @param rowNumbers     the row numbers
352
     * @param ifVersionAfter only fetch Sheet if more recent version available
353
     * @param level          compatibility level
354
     * @return the sheet resource (note that if there is no such resource, this method will throw
355
     * ResourceNotFoundException rather than returning null).
356
     * @throws IllegalArgumentException    if any argument is null or empty string
357
     * @throws InvalidRequestException     if there is any problem with the REST API request
358
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
359
     * @throws ResourceNotFoundException   if the resource cannot be found
360
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
361
     * @throws SmartsheetException         if there is any other error during the operation
362
     */
363
    public Sheet getSheet(
364
            long id,
365
            EnumSet<SheetInclusion> includes,
366
            EnumSet<ObjectExclusion> excludes,
367
            Set<Long> rowIds,
368
            Set<Integer> rowNumbers,
369
            Set<Long> columnIds,
370
            Integer pageSize,
371
            Integer page,
372
            Integer ifVersionAfter,
373
            Integer level
374
    ) throws SmartsheetException {
375

376
        String path = SHEETS + "/" + id;
1✔
377

378
        // Add the parameters to a map and build the query string at the end
379
        Map<String, Object> parameters = new HashMap<>();
1✔
380

381
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
382
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
383
        parameters.put("rowIds", QueryUtil.generateCommaSeparatedList(rowIds));
1✔
384
        parameters.put("rowNumbers", QueryUtil.generateCommaSeparatedList(rowNumbers));
1✔
385
        parameters.put("columnIds", QueryUtil.generateCommaSeparatedList(columnIds));
1✔
386
        parameters.put("pageSize", pageSize);
1✔
387
        parameters.put("page", page);
1✔
388
        parameters.put("ifVersionAfter", ifVersionAfter);
1✔
389
        parameters.put("level", level);
1✔
390

391
        // Iterate through the map of parameters and generate the query string
392
        path += QueryUtil.generateUrl(null, parameters);
1✔
393

394
        return this.getResource(path, Sheet.class);
1✔
395
    }
396

397
    /**
398
     * <p>Get sheet by ID. Load all row and column data</p>
399
     *
400
     * @param sheetId id of the sheet to fetch
401
     * @return the sheet resource (note that if there is no such resource, this method will throw
402
     * ResourceNotFoundException rather than returning null).
403
     * @throws IllegalArgumentException    if any argument is null or empty string
404
     * @throws InvalidRequestException     if there is any problem with the REST API request
405
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
406
     * @throws ResourceNotFoundException   if the resource cannot be found
407
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
408
     * @throws SmartsheetException         if there is any other error during the operation
409
     */
410
    public Sheet getSheet(long sheetId) throws SmartsheetException {
411
        return getSheet(sheetId, null, null, null, null, null, null, null, null, null);
1✔
412
    }
413

414
    /**
415
     * Get a sheet as an Excel file.
416
     * <p>
417
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept
418
     * HTTP header
419
     *
420
     * @param id           the id
421
     * @param outputStream the OutputStream to which the Excel file will be written
422
     * @throws IllegalArgumentException    : if outputStream is null
423
     * @throws InvalidRequestException     : if there is any problem with the REST API request
424
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
425
     * @throws ResourceNotFoundException   : if the resource can not be found
426
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
427
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
428
     * @throws SmartsheetException         : if there is any other error occurred during the operation
429
     */
430
    public void getSheetAsExcel(long id, OutputStream outputStream) throws SmartsheetException {
431
        getSheetAsFile(id, null, outputStream, "application/vnd.ms-excel");
1✔
432
    }
1✔
433

434
    /**
435
     * Get a sheet as a PDF file.
436
     * <p>
437
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/pdf" Accept HTTP
438
     * header
439
     *
440
     * @param id           the id
441
     * @param outputStream the output stream to which the PDF file will be written.
442
     * @param paperSize    the optional paper size
443
     * @throws IllegalArgumentException    : if outputStream is null
444
     * @throws InvalidRequestException     : if there is any problem with the REST API request
445
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
446
     * @throws ResourceNotFoundException   : if the resource can not be found
447
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
448
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
449
     * @throws SmartsheetException         : if there is any other error occurred during the operation
450
     */
451
    public void getSheetAsPDF(long id, OutputStream outputStream, PaperSize paperSize) throws SmartsheetException {
452
        getSheetAsFile(id, paperSize, outputStream, "application/pdf");
1✔
453
    }
1✔
454

455
    /**
456
     * Create a sheet in default "Sheets" collection.
457
     * <p>
458
     * It mirrors to the following Smartsheet REST API method: POST /sheets
459
     *
460
     * @param sheet the sheet to create, limited to the following required attributes: * name (string) *
461
     *              columns (array of Column objects, limited to the following attributes) - title - primary - type - symbol -
462
     *              options
463
     * @return the created sheet
464
     * @throws IllegalArgumentException    : if any argument is null
465
     * @throws InvalidRequestException     : if there is any problem with the REST API request
466
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
467
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
468
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
469
     * @throws SmartsheetException         : if there is any other error occurred during the operation
470
     */
471
    public Sheet createSheet(Sheet sheet) throws SmartsheetException {
472
        return this.createResource(SHEETS, Sheet.class, sheet);
1✔
473
    }
474

475
    /**
476
     * Create a sheet (from existing sheet or template) in default "Sheets" collection.
477
     * <p>
478
     * It mirrors to the following Smartsheet REST API method: POST /sheets
479
     *
480
     * @param sheet    the sheet to create, limited to the following required attributes: * name (string) * fromId
481
     *                 (number): ID of the Sheet or Template from which to create the sheet.
482
     * @param includes used to specify the optional objects to include, currently DATA, DISCUSSIONS and ATTACHMENTS are supported.
483
     * @return the sheet
484
     * @throws IllegalArgumentException    : if any argument is null
485
     * @throws InvalidRequestException     : if there is any problem with the REST API request
486
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
487
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
488
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
489
     * @throws SmartsheetException         : if there is any other error occurred during the operation
490
     */
491
    public Sheet createSheetFromTemplate(Sheet sheet, EnumSet<SheetTemplateInclusion> includes) throws SmartsheetException {
492
        Map<String, Object> parameters = new HashMap<>();
1✔
493
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
494
        String path = QueryUtil.generateUrl(SHEETS, parameters);
1✔
495

496
        return this.createResource(path, Sheet.class, sheet);
1✔
497
    }
498

499
    /**
500
     * Imports a sheet.
501
     * <p>
502
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
503
     *
504
     * @param file               path to the CSV file
505
     * @param sheetName          destination sheet name
506
     * @param headerRowIndex     index (0 based) of row to be used for column names
507
     * @param primaryColumnIndex index (0 based) of primary column
508
     * @return the created sheet
509
     * @throws IllegalArgumentException    if any argument is null or empty string
510
     * @throws InvalidRequestException     if there is any problem with the REST API request
511
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
512
     * @throws ResourceNotFoundException   if the resource cannot be found
513
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
514
     * @throws SmartsheetException         if there is any other error during the operation
515
     */
516
    public Sheet importCsv(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
517
        return importFile("sheets/import", file, TEXT_CSV, sheetName, headerRowIndex, primaryColumnIndex);
×
518
    }
519

520
    /**
521
     * Imports a sheet.
522
     * <p>
523
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
524
     *
525
     * @param file               path to the XLSX file
526
     * @param sheetName          destination sheet name
527
     * @param headerRowIndex     index (0 based) of row to be used for column names
528
     * @param primaryColumnIndex index (0 based) of primary column
529
     * @return the created sheet
530
     * @throws IllegalArgumentException    if any argument is null or empty string
531
     * @throws InvalidRequestException     if there is any problem with the REST API request
532
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
533
     * @throws ResourceNotFoundException   if the resource cannot be found
534
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
535
     * @throws SmartsheetException         if there is any other error during the operation
536
     */
537
    public Sheet importXlsx(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
538
        return importFile("sheets/import", file, XLSX_CONTENT_TYPE,
×
539
                sheetName, headerRowIndex, primaryColumnIndex);
540
    }
541

542
    /**
543
     * Create a sheet in given folder.
544
     * <p>
545
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets
546
     *
547
     * @param folderId the folder id
548
     * @param sheet    the sheet to create, limited to the following required
549
     *                 attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
550
     *                 primary - type - symbol - options
551
     * @return the created sheet
552
     * @throws IllegalArgumentException    : if any argument is null
553
     * @throws InvalidRequestException     : if there is any problem with the REST API request
554
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
555
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
556
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
557
     * @throws SmartsheetException         : if there is any other error occurred during the operation
558
     */
559
    public Sheet createSheetInFolder(long folderId, Sheet sheet) throws SmartsheetException {
560

561
        return this.createResource(FOLDERS + "/" + folderId + "/" + SHEETS, Sheet.class, sheet);
1✔
562
    }
563

564
    /**
565
     * Create a sheet in given folder.
566
     * <p>
567
     * It mirrors to the following Smartsheet REST API method: POST /folder/{folderId}/sheets
568
     *
569
     * @param folderId the folder id
570
     * @param sheet    the sheet
571
     * @param includes the includes
572
     * @return the sheet to create, limited to the following required
573
     * attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
574
     * primary - type - symbol - options
575
     * @throws IllegalArgumentException    : if any argument is null
576
     * @throws InvalidRequestException     : if there is any problem with the REST API request
577
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
578
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
579
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
580
     * @throws SmartsheetException         : if there is any other error occurred during the operation
581
     */
582
    public Sheet createSheetInFolderFromTemplate(
583
            long folderId,
584
            Sheet sheet,
585
            EnumSet<SheetTemplateInclusion> includes
586
    ) throws SmartsheetException {
587
        Map<String, Object> parameters = new HashMap<>();
1✔
588
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
589
        String path = QueryUtil.generateUrl(FOLDERS + "/" + folderId + "/" + SHEETS, parameters);
1✔
590

591
        return this.createResource(path, Sheet.class, sheet);
1✔
592
    }
593

594
    /**
595
     * Imports a sheet in given folder.
596
     * <p>
597
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
598
     *
599
     * @param folderId           the folder id
600
     * @param file               path to the CSV file
601
     * @param sheetName          destination sheet name
602
     * @param headerRowIndex     index (0 based) of row to be used for column names
603
     * @param primaryColumnIndex index (0 based) of primary column
604
     * @return the created sheet
605
     * @throws IllegalArgumentException    if any argument is null or empty string
606
     * @throws InvalidRequestException     if there is any problem with the REST API request
607
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
608
     * @throws ResourceNotFoundException   if the resource cannot be found
609
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
610
     * @throws SmartsheetException         if there is any other error during the operation
611
     */
612
    public Sheet importCsvInFolder(
613
            long folderId,
614
            String file,
615
            String sheetName,
616
            Integer headerRowIndex,
617
            Integer primaryColumnIndex
618
    ) throws SmartsheetException {
619
        return importFile(
×
620
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
621
                file,
622
                TEXT_CSV,
623
                sheetName,
624
                headerRowIndex,
625
                primaryColumnIndex
626
        );
627
    }
628

629
    /**
630
     * Imports a sheet in given folder.
631
     * <p>
632
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
633
     *
634
     * @param folderId           the folder id
635
     * @param file               path to the XLSX file
636
     * @param sheetName          destination sheet name
637
     * @param headerRowIndex     index (0 based) of row to be used for column names
638
     * @param primaryColumnIndex index (0 based) of primary column
639
     * @return the created sheet
640
     * @throws IllegalArgumentException    if any argument is null or empty string
641
     * @throws InvalidRequestException     if there is any problem with the REST API request
642
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
643
     * @throws ResourceNotFoundException   if the resource cannot be found
644
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
645
     * @throws SmartsheetException         if there is any other error during the operation
646
     */
647
    public Sheet importXlsxInFolder(
648
            long folderId,
649
            String file,
650
            String sheetName,
651
            Integer headerRowIndex,
652
            Integer primaryColumnIndex
653
    ) throws SmartsheetException {
654
        return importFile(
×
655
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
656
                file,
657
                XLSX_CONTENT_TYPE,
658
                sheetName,
659
                headerRowIndex,
660
                primaryColumnIndex
661
        );
662
    }
663

664
    /**
665
     * Create a sheet in given workspace.
666
     * <p>
667
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
668
     *
669
     * @param workspaceId the workspace id
670
     * @param sheet       the sheet to create, limited to the following required attributes: * name (string) * columns
671
     *                    (array of Column objects, limited to the following attributes) - title - primary - type - symbol - options
672
     * @return the created sheet
673
     * @throws IllegalArgumentException    : if any argument is null
674
     * @throws InvalidRequestException     : if there is any problem with the REST API request
675
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
676
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
677
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
678
     * @throws SmartsheetException         : if there is any other error occurred during the operation
679
     */
680
    public Sheet createSheetInWorkspace(long workspaceId, Sheet sheet) throws SmartsheetException {
681
        return this.createResource(WORKSPACES + "/" + workspaceId + "/" + SHEETS, Sheet.class, sheet);
1✔
682
    }
683

684
    /**
685
     * Create a sheet (from existing sheet or template) in given workspace.
686
     * <p>
687
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
688
     *
689
     * @param workspaceId the workspace id
690
     * @param sheet       the sheet to create, limited to the following required attributes:
691
     *                    <ul>
692
     *                        <li>name (string)</li>
693
     *                        <li>fromId (number): ID of the Sheet or Template from which to create the sheet.</li>
694
     *                        <li>includes : used to specify the optional objects to include;
695
     *                        currently DATA, DISCUSSIONS and ATTACHMENTS are supported.</li>
696
     *                    </ul>
697
     * @param includes    the includes
698
     * @return the created sheet
699
     * @throws IllegalArgumentException    : if any argument is null
700
     * @throws InvalidRequestException     : if there is any problem with the REST API request
701
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
702
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
703
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
704
     * @throws SmartsheetException         : if there is any other error occurred during the operation
705
     */
706
    public Sheet createSheetInWorkspaceFromTemplate(long workspaceId, Sheet sheet, EnumSet<SheetTemplateInclusion> includes)
707
            throws SmartsheetException {
708
        Map<String, Object> parameters = new HashMap<>();
1✔
709
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
710
        String path = QueryUtil.generateUrl(WORKSPACES + "/" + workspaceId + "/" + SHEETS, parameters);
1✔
711

712
        return this.createResource(path, Sheet.class, sheet);
1✔
713
    }
714

715
    /**
716
     * Imports a sheet in given workspace.
717
     * <p>
718
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
719
     *
720
     * @param workspaceId        the workspace id
721
     * @param file               path to the CSV file
722
     * @param sheetName          destination sheet name
723
     * @param headerRowIndex     index (0 based) of row to be used for column names
724
     * @param primaryColumnIndex index (0 based) of primary column
725
     * @return the created sheet
726
     * @throws IllegalArgumentException    if any argument is null or empty string
727
     * @throws InvalidRequestException     if there is any problem with the REST API request
728
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
729
     * @throws ResourceNotFoundException   if the resource cannot be found
730
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
731
     * @throws SmartsheetException         if there is any other error during the operation
732
     */
733
    public Sheet importCsvInWorkspace(
734
            long workspaceId,
735
            String file,
736
            String sheetName,
737
            Integer headerRowIndex,
738
            Integer primaryColumnIndex
739
    ) throws SmartsheetException {
740
        return importFile(
×
741
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
742
                file,
743
                TEXT_CSV,
744
                sheetName,
745
                headerRowIndex,
746
                primaryColumnIndex
747
        );
748
    }
749

750
    /**
751
     * Imports a sheet in given workspace.
752
     * <p>
753
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
754
     *
755
     * @param workspaceId        the workspace id
756
     * @param file               path to the XLSX file
757
     * @param sheetName          destination sheet name
758
     * @param headerRowIndex     index (0 based) of row to be used for column names
759
     * @param primaryColumnIndex index (0 based) of primary column
760
     * @return the created sheet
761
     * @throws IllegalArgumentException    if any argument is null or empty string
762
     * @throws InvalidRequestException     if there is any problem with the REST API request
763
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
764
     * @throws ResourceNotFoundException   if the resource cannot be found
765
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
766
     * @throws SmartsheetException         if there is any other error during the operation
767
     */
768
    public Sheet importXlsxInWorkspace(
769
            long workspaceId,
770
            String file,
771
            String sheetName,
772
            Integer headerRowIndex,
773
            Integer primaryColumnIndex
774
    ) throws SmartsheetException {
775
        return importFile(
×
776
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
777
                file,
778
                XLSX_CONTENT_TYPE,
779
                sheetName,
780
                headerRowIndex,
781
                primaryColumnIndex
782
        );
783
    }
784

785
    /**
786
     * Delete a sheet.
787
     * <p>
788
     * It mirrors to the following Smartsheet REST API method: DELETE /sheet{id}
789
     *
790
     * @param id the ID of the sheet
791
     * @throws InvalidRequestException     : if there is any problem with the REST API request
792
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
793
     * @throws ResourceNotFoundException   : if the resource can not be found
794
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
795
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
796
     * @throws SmartsheetException         : if there is any other error occurred during the operation
797
     */
798
    public void deleteSheet(long id) throws SmartsheetException {
799
        this.deleteResource(SHEETS + "/" + id, Sheet.class);
1✔
800
    }
1✔
801

802
    /**
803
     * Update a sheet.
804
     * <p>
805
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{id}
806
     *
807
     * @param sheet the sheet to update
808
     * @return the updated sheet
809
     * @throws IllegalArgumentException    if any argument is null or empty string
810
     * @throws InvalidRequestException     if there is any problem with the REST API request
811
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
812
     * @throws ResourceNotFoundException   if the resource cannot be found
813
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
814
     * @throws SmartsheetException         if there is any other error during the operation
815
     */
816
    public Sheet updateSheet(Sheet sheet) throws SmartsheetException {
817
        return this.updateResource(SHEETS + "/" + sheet.getId(), Sheet.class, sheet);
1✔
818
    }
819

820
    /**
821
     * Get a sheet version.
822
     * <p>
823
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}/version
824
     *
825
     * @param id the id
826
     * @return the sheet version (note that if there is no such resource, this method will throw
827
     * ResourceNotFoundException rather than returning null).
828
     * @throws InvalidRequestException     : if there is any problem with the REST API request
829
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
830
     * @throws ResourceNotFoundException   : if the resource can not be found
831
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
832
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
833
     * @throws SmartsheetException         : if there is any other error occurred during the operation
834
     */
835
    public int getSheetVersion(long id) throws SmartsheetException {
836
        return this.getResource(SHEETS + "/" + id + "/version", Sheet.class).getVersion();
1✔
837
    }
838

839
    /**
840
     * Send a sheet as a PDF attachment via email to the designated recipients.
841
     * <p>
842
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/emails
843
     *
844
     * @param id    the id
845
     * @param email the email
846
     * @throws IllegalArgumentException    : if any argument is null
847
     * @throws InvalidRequestException     : if there is any problem with the REST API request
848
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
849
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
850
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
851
     * @throws SmartsheetException         : if there is any other error occurred during the operation
852
     */
853
    public void sendSheet(long id, SheetEmail email) throws SmartsheetException {
854
        this.createResource(SHEETS + "/" + id + "/emails", SheetEmail.class, email);
1✔
855
    }
1✔
856

857
    /**
858
     * Get a sheet as an Excel file.
859
     * <p>
860
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept HTTP header
861
     *
862
     * @param id           the id of the sheet
863
     * @param outputStream the output stream to which the Excel file will be written.
864
     * @throws IllegalArgumentException    if any argument is null or empty string
865
     * @throws InvalidRequestException     if there is any problem with the REST API request
866
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
867
     * @throws ResourceNotFoundException   if the resource cannot be found
868
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
869
     * @throws SmartsheetException         if there is any other error during the operation
870
     */
871
    public void getSheetAsCSV(long id, OutputStream outputStream) throws SmartsheetException {
872
        getSheetAsFile(id, null, outputStream, TEXT_CSV);
×
873
    }
×
874

875
    /**
876
     * Get the status of the Publish settings of the sheet, including the URLs of any enabled publishings.
877
     * <p>
878
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{sheetId}/publish
879
     *
880
     * @param id the id
881
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
882
     * * rather than returning null).
883
     * @throws InvalidRequestException     : if there is any problem with the REST API request
884
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
885
     * @throws ResourceNotFoundException   : if the resource can not be found
886
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
887
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
888
     * @throws SmartsheetException         : if there is any other error occurred during the operation
889
     */
890
    public SheetPublish getPublishStatus(long id) throws SmartsheetException {
891
        return this.getResource(SHEETS + "/" + id + "/publish", SheetPublish.class);
1✔
892
    }
893

894
    /**
895
     * Sets the publish status of a sheet and returns the new status, including the URLs of any enabled publishings.
896
     * <p>
897
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{sheetId}/publish
898
     *
899
     * @param id      the id
900
     * @param publish the SheetPublish object limited to the following attributes *
901
     *                readOnlyLiteEnabled * readOnlyFullEnabled * readWriteEnabled * icalEnabled
902
     * @return the updated SheetPublish (note that if there is no such resource, this method will throw
903
     * ResourceNotFoundException rather than returning null).
904
     * @throws IllegalArgumentException    : if any argument is null
905
     * @throws InvalidRequestException     : if there is any problem with the REST API request
906
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
907
     * @throws ResourceNotFoundException   : if the resource can not be found
908
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
909
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
910
     * @throws SmartsheetException         : if there is any other error occurred during the operation
911
     */
912
    public SheetPublish updatePublishStatus(long id, SheetPublish publish) throws SmartsheetException {
913
        return this.updateResource(SHEETS + "/" + id + "/publish", SheetPublish.class, publish);
1✔
914
    }
915

916
    /**
917
     * Creates a copy of the specified sheet.
918
     * <p>
919
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
920
     *
921
     * @param sheetId              the sheet id
922
     * @param containerDestination describes the destination container
923
     * @param includes             optional parameters to include
924
     * @return the sheet
925
     * @throws IllegalArgumentException    : if folder is null
926
     * @throws InvalidRequestException     : if there is any problem with the REST API request
927
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
928
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
929
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
930
     * @throws SmartsheetException         : if there is any other error occurred during the operation
931
     */
932
    public Sheet copySheet(
933
            long sheetId,
934
            ContainerDestination containerDestination,
935
            EnumSet<SheetCopyInclusion> includes
936
    ) throws SmartsheetException {
937
        return copySheet(sheetId, containerDestination, includes, null);
1✔
938
    }
939

940
    /**
941
     * Creates a copy of the specified sheet.
942
     * <p>
943
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
944
     *
945
     * @param sheetId              the sheet id
946
     * @param containerDestination describes the destination container
947
     * @param includes             optional parameters to include
948
     * @param excludes             optional parameters to exclude
949
     * @return the sheet
950
     * @throws IllegalArgumentException    : if folder is null
951
     * @throws InvalidRequestException     : if there is any problem with the REST API request
952
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
953
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
954
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
955
     * @throws SmartsheetException         : if there is any other error occurred during the operation
956
     */
957
    public Sheet copySheet(
958
            long sheetId,
959
            ContainerDestination containerDestination,
960
            EnumSet<SheetCopyInclusion> includes,
961
            EnumSet<CopyExclusion> excludes
962
    ) throws SmartsheetException {
963

964
        String path = SHEETS + "/" + sheetId + "/copy";
1✔
965
        Map<String, Object> parameters = new HashMap<>();
1✔
966

967
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
968
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
969

970
        path += QueryUtil.generateUrl(null, parameters);
1✔
971

972
        return this.createResource(path, Sheet.class, containerDestination);
1✔
973
    }
974

975
    /**
976
     * Moves the specified Sheet to another location.
977
     * <p>
978
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/move
979
     *
980
     * @param sheetId              the folder id
981
     * @param containerDestination describes the destination container
982
     * @return the sheet
983
     * @throws IllegalArgumentException    : if folder is null
984
     * @throws InvalidRequestException     : if there is any problem with the REST API request
985
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
986
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
987
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
988
     * @throws SmartsheetException         : if there is any other error occurred during the operation
989
     */
990
    public Sheet moveSheet(long sheetId, ContainerDestination containerDestination) throws SmartsheetException {
991

992
        String path = SHEETS + "/" + sheetId + "/move";
1✔
993
        return this.createResource(path, Sheet.class, containerDestination);
1✔
994
    }
995

996
    /**
997
     * Creates an Update Request for the specified Row(s) within the Sheet.
998
     * <p>
999
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/updaterequests
1000
     *
1001
     * @param sheetId the sheet id
1002
     * @param email   the email
1003
     * @return the update request object
1004
     * @throws IllegalArgumentException    : if any argument is null
1005
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1006
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1007
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1008
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1009
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1010
     */
1011
    public UpdateRequest createUpdateRequest(long sheetId, MultiRowEmail email) throws SmartsheetException {
1012
        return this.createResource(SHEETS + "/" + sheetId + "/updaterequests", UpdateRequest.class, email);
1✔
1013
    }
1014

1015
    /**
1016
     * Sort a sheet according to the sort criteria.
1017
     * <p>
1018
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
1019
     *
1020
     * @param sheetId       the sheet id
1021
     * @param sortSpecifier the sort criteria
1022
     * @return the update request object
1023
     * @throws IllegalArgumentException    : if any argument is null
1024
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1025
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1026
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1027
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1028
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1029
     */
1030
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier) throws SmartsheetException {
1031
        return this.sortSheet(sheetId, sortSpecifier, null);
1✔
1032
    }
1033

1034
    /**
1035
     * Sort a sheet according to the sort criteria.
1036
     * <p>
1037
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
1038
     *
1039
     * @param sheetId       the sheet id
1040
     * @param sortSpecifier the sort criteria
1041
     * @param level         compatibility level
1042
     * @return the update request object
1043
     * @throws SmartsheetException         the smartsheet exception
1044
     * @throws IllegalArgumentException    : if any argument is null
1045
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1046
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1047
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1048
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1049
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1050
     */
1051
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier, Integer level) throws SmartsheetException {
1052
        Util.throwIfNull(sortSpecifier);
1✔
1053

1054
        String path = SHEETS + "/" + sheetId + "/sort";
1✔
1055
        if (level != null) {
1✔
1056
            path += "?level=" + level;
×
1057
        }
1058

1059
        HttpRequest request = createHttpRequest(smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
1✔
1060

1061
        ByteArrayOutputStream objectBytesStream = new ByteArrayOutputStream();
1✔
1062
        this.smartsheet.getJsonSerializer().serialize(sortSpecifier, objectBytesStream);
1✔
1063

1064
        HttpEntity entity = new HttpEntity();
1✔
1065
        entity.setContentType("application/json");
1✔
1066
        entity.setContent(new ByteArrayInputStream(objectBytesStream.toByteArray()));
1✔
1067
        entity.setContentLength(objectBytesStream.size());
1✔
1068
        request.setEntity(entity);
1✔
1069

1070
        Sheet obj = null;
1✔
1071
        try {
1072
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
1✔
1073
            switch (response.getStatusCode()) {
1✔
1074
                case 200: {
1075
                    InputStream inputStream = response.getEntity().getContent();
1✔
1076
                    try {
1077
                        obj = this.smartsheet.getJsonSerializer().deserialize(Sheet.class, inputStream);
1✔
1078
                    } catch (IOException e) {
×
1079
                        throw new SmartsheetException(e);
×
1080
                    }
1✔
1081
                    break;
1082
                }
1083
                default:
1084
                    handleError(response);
×
1085
            }
1086
        } finally {
1087
            smartsheet.getHttpClient().releaseConnection();
1✔
1088
        }
1089
        return obj;
1✔
1090
    }
1091

1092
    /**
1093
     * Return the SheetRowResources object that provides access to Row resources associated with Sheet resources.
1094
     *
1095
     * @return the sheet row resources
1096
     */
1097
    public SheetRowResources rowResources() {
1098
        return this.rows;
1✔
1099
    }
1100

1101
    /**
1102
     * Return the SheetColumnResources object that provides access to Column resources associated with Sheet resources.
1103
     *
1104
     * @return the sheet column resources
1105
     */
1106
    public SheetColumnResources columnResources() {
1107
        return this.columns;
1✔
1108
    }
1109

1110
    /**
1111
     * Return the AssociatedAttachmentResources object that provides access to attachment resources associated with
1112
     * Sheet resources.
1113
     *
1114
     * @return the associated attachment resources
1115
     */
1116
    public SheetAttachmentResources attachmentResources() {
1117
        return this.attachments;
×
1118
    }
1119

1120
    /**
1121
     * Return the AssociatedDiscussionResources object that provides access to discussion resources associated with
1122
     * Sheet resources.
1123
     *
1124
     * @return the associated discussion resources
1125
     */
1126
    public SheetDiscussionResources discussionResources() {
1127
        return this.discussions;
1✔
1128
    }
1129

1130
    /**
1131
     * Return the SheetCommentResources object that provides access to discussion resources associated with
1132
     * Sheet resources.
1133
     *
1134
     * @return the associated comment resources
1135
     */
1136
    public SheetCommentResources commentResources() {
1137
        return this.comments;
×
1138
    }
1139

1140
    /**
1141
     * Return the SheetUpdateRequestResources object that provides access to update request resources
1142
     * associated with Sheet resources.
1143
     *
1144
     * @return the associated update request resources
1145
     */
1146
    public SheetUpdateRequestResources updateRequestResources() {
1147
        return this.updateRequests;
×
1148
    }
1149

1150
    /**
1151
     * Return the SheetFilterResources object that provides access to sheet filter resources
1152
     * associated with Sheet resources.
1153
     *
1154
     * @return the associated sheet filter resources
1155
     */
1156
    public SheetFilterResources filterResources() {
1157
        return this.filters;
×
1158
    }
1159

1160
    /**
1161
     * Return the SheetAutomationRuleResources object that provides access to automation rule resources
1162
     * associated with the Sheet resources.
1163
     *
1164
     * @return the associated automation rule resources
1165
     */
1166
    public SheetAutomationRuleResources automationRuleResources() {
1167
        return automationRules;
×
1168
    }
1169

1170
    /**
1171
     * Return the SheetCrossSheetReferenceResources object that provides access to the cross sheet reference resources
1172
     * associated with the Sheet resources.
1173
     *
1174
     * @return the cross sheet reference resources
1175
     */
1176
    public SheetCrossSheetReferenceResources crossSheetReferenceResources() {
1177
        return crossSheetReferences;
×
1178
    }
1179

1180
    /**
1181
     * Return the SheetSummaryResources object that provides access to the sheet summary resources
1182
     * associated with the Sheet resources.
1183
     *
1184
     * @return the sheet summary resources
1185
     */
1186
    public SheetSummaryResources summaryResources() {
1187
        return sheetSummary;
×
1188
    }
1189

1190
    /**
1191
     * Internal function used by all the import routines
1192
     *
1193
     * @param path               endpoint for import
1194
     * @param file               full path to file
1195
     * @param contentType        content type of the file being imported (either CSV or XLSX)
1196
     * @param sheetName          sheetName from caller (can be null)
1197
     * @param headerRowIndex     headerRowIndex from caller (can be null)
1198
     * @param primaryColumnIndex primaryColumnIndex from caller (can be null)
1199
     * @return the new imported sheet
1200
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1201
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1202
     * @throws ResourceNotFoundException   : if the resource can not be found
1203
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1204
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1205
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1206
     */
1207
    private Sheet importFile(String path, String file, String contentType, String sheetName, Integer headerRowIndex,
1208
                             Integer primaryColumnIndex) throws SmartsheetException {
1209
        Util.throwIfNull(path, file, contentType);
×
1210
        Util.throwIfEmpty(path, file, contentType);
×
1211

1212
        File f = new File(file);
×
1213
        Map<String, Object> parameters = new HashMap<>();
×
1214
        if (sheetName == null) {
×
1215
            sheetName = f.getName();
×
1216
        }
1217
        parameters.put("sheetName", sheetName);
×
1218
        parameters.put("headerRowIndex", headerRowIndex);
×
1219
        parameters.put("primaryColumnIndex", primaryColumnIndex);
×
1220
        path = QueryUtil.generateUrl(path, parameters);
×
1221
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
1222
        request.getHeaders().put("Content-Disposition", "attachment");
×
1223
        request.getHeaders().put("Content-Type", contentType);
×
1224

1225
        InputStream is = null;
×
1226
        try {
1227
            is = new FileInputStream(f);
×
1228
        } catch (FileNotFoundException e) {
×
1229
            throw new SmartsheetException(e);
×
1230
        }
×
1231

1232
        HttpEntity entity = new HttpEntity();
×
1233
        entity.setContentType(contentType);
×
1234
        entity.setContent(is);
×
1235
        entity.setContentLength(f.length());
×
1236
        request.setEntity(entity);
×
1237

1238
        Sheet obj = null;
×
1239
        try {
1240
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
1241
            switch (response.getStatusCode()) {
×
1242
                case 200:
1243
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(Sheet.class,
×
1244
                            response.getEntity().getContent()).getResult();
×
1245
                    break;
×
1246
                default:
1247
                    handleError(response);
×
1248
            }
1249
        } finally {
1250
            smartsheet.getHttpClient().releaseConnection();
×
1251
        }
1252

1253
        return obj;
×
1254
    }
1255

1256
    /**
1257
     * Get a sheet as a file.
1258
     * <p>
1259
     *
1260
     * @param id           the id
1261
     * @param paperSize    the paper size
1262
     * @param outputStream the OutputStream to which the Excel file will be written
1263
     * @param contentType  the content type
1264
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1265
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1266
     * @throws ResourceNotFoundException   : if the resource can not be found
1267
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1268
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1269
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1270
     */
1271
    private void getSheetAsFile(long id, PaperSize paperSize, OutputStream outputStream, String contentType)
1272
            throws SmartsheetException {
1273
        Util.throwIfNull(outputStream, contentType);
1✔
1274

1275
        String path = SHEETS + "/" + id;
1✔
1276
        if (paperSize != null) {
1✔
1277
            path += "?paperSize=" + paperSize;
1✔
1278
        }
1279

1280
        HttpRequest request;
1281
        request = createHttpRequest(this.getSmartsheet().getBaseURI().resolve(path), HttpMethod.GET);
1✔
1282
        request.getHeaders().put("Accept", contentType);
1✔
1283

1284
        com.smartsheet.api.internal.http.HttpResponse response = getSmartsheet().getHttpClient().request(request);
1✔
1285

1286
        switch (response.getStatusCode()) {
1✔
1287
            case 200:
1288
                try {
1289
                    copyStream(response.getEntity().getContent(), outputStream);
1✔
1290
                } catch (IOException e) {
×
1291
                    throw new SmartsheetException(e);
×
1292
                }
1✔
1293
                break;
1294
            default:
1295
                handleError(response);
×
1296
        }
1297

1298
        getSmartsheet().getHttpClient().releaseConnection();
1✔
1299
    }
1✔
1300

1301
    /**
1302
     * Get the path of a sheet (workspace/folder hierarchy).
1303
     * <p>
1304
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{sheetId}/path
1305
     *
1306
     * @param sheetId the sheet id
1307
     * @return the container path
1308
     * @throws SmartsheetException the smartsheet exception
1309
     */
1310
    @Override
1311
    public SheetPathNode getSheetPath(long sheetId) throws SmartsheetException {
1312
        return this.getResource(SHEETS + "/" + sheetId + "/path", SheetPathNode.class);
1✔
1313
    }
1314

1315
    /**
1316
     * Copy stream.
1317
     *
1318
     * @param input  the input
1319
     * @param output the output
1320
     * @throws IOException Signals that an I/O exception has occurred.
1321
     * @deprecated replace with StreamUtil.copyContentIntoOutputStream()
1322
     */
1323
    @Deprecated(since = "2.0.0", forRemoval = true)
1324
    private static void copyStream(InputStream input, OutputStream output) throws IOException {
1325
        byte[] buffer = new byte[BUFFER_SIZE];
1✔
1326
        int len;
1327
        while ((len = input.read(buffer)) != -1) {
1✔
1328
            output.write(buffer, 0, len);
1✔
1329
        }
1330
    }
1✔
1331

1332
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc