Search code examples
javascriptsmartsheet-apismartsheet-api-2.0

How to add a new row to Smartsheet with a column of type Dropdown List and set multiple options as true


I am working with the SmartSheets API and more specifically the "Adding Rows to Sheet" portion as documented Here

My problem is that i am having issues creating a cell object that shows multiple active picklist options when displayed on the sheet.

2 different picklist formats

As seen in the above image there are 2 different formats for the Dropdown List column type.

On top we can clearly see test test2 test3 being separated into their own distinct options. This is the format that is given when you pick/type in an option from smartsheet itself.

Below it is the result i receive when i attempt to use the API as documented to insert values for a Dropdown List.

The documentation from what i can tell fails to mention how to handle different column types. My code appears as follows to obtain the bottom (incorrect) version. (Note Sheet and Column IDs removed)

function SendNewSmartSheetRow(SheetID){
    
        let RequestHeaders = new Headers()
        RequestHeaders.append("Authorization", `Bearer ${SmartSheetAuth}`)
        RequestHeaders.append("Content-Type", "application/json")

        let RequestBody = {
            toBottom: true,
            cells: [
                {
                    columnId: columnId,
                    values: "test test2 test3"
                }
            ],
        }

        let RequestInit = {
            method: "POST",
            headers: RequestHeaders,
            body: JSON.stringify(RequestBody)
        }
        let Request = fetch(`${SmartSheetBaseURL}/sheets/${SheetID}/rows`, RequestInit)
        .then((res)=>{return(res.json())})

        return(Request)

}
let a = await SendNewSmartSheetRow(SheetId)
console.log(a)

In the code above the only part of interest really is the creating of the requestBody. Below is my original version which evidently failed.

let RequestBody = {
            toBottom: true,
            cells: [
                {
                    columnId: columnId,
                    values: "test test2 test3"
                }
            ],
        }

I had also done some googling and attempted a couple of different options. Of interest of these options is the following request body

let RequestBody = {
            toBottom: true,
            cells: [
                {
                    columnId: 8839694736379780,
                    values: "test test2 test3",
                    objectValue:{
                        objectType:"MULTI_PICKLIST",
                        multiPickList:{
                            values:["test","test2","test3"]
                        },
                    },
                }
            ],
        }

Interestingly enough this prompted me with the response error Required object attribute(s) are missing from your request: multiPickList.values[]. but clearly that is seen, so i attempted to add it to the base cell object and not objectValue and was met with the same error.

How am i supposed to add multiple options as true when creating a new row with a Dropdown List column type?


Solution

  • You were on the right track with the final snippet you included in your post, but just not quite there. I agree with you that this scenario is not very well documented in the Smartsheet API documentation.

    The following snippet shows an API request that adds one new row to a sheet and populates one cell in that new row -- a multi-picklist cell -- with 3 values: test, test2, and test3.

    POST https://api.smartsheet.com/2.0/sheets/2702602705784708/rows
    
    [
        {
            "cells": [
                {
                    "columnId": "8429111750905732", 
                    "objectValue": {
                        "objectType": "MULTI_PICKLIST",
                        "values": ["test", "test2", "test3"]
                    }
                }
                
            ]
        }
    ]
    

    Here's the result of that API request in Smartsheet -- which seems to be the outcome you're seeking:

    smartsheet sheet

    Finally, a couple of notes:

    • You don't need to set the isBottom attribute to true in an Add Row(s) request, as adding rows to the bottom of the sheet is the default behavior if no location-specifier attribute is included in an Add Row(s) request. (See the API docs for details about location specifier attributes: https://smartsheet.redoc.ly/tag/rowsRelated/#section/Specify-Row-Location.)

    • The Add Row(s) endpoint you're calling is designed to let you add multiple rows with a single request -- which means the request body should be an array of Row objects (as shown in my request). Technically, it'll accept (without error) a request body that just specifies a single Row object (not in an array), so what you're doing will work. But if you ever want to add multiple rows with a single API request, you'll need to specify an array of Row objects in the request body.