Search code examples
python-3.xsmartsheet-api

How to add a new row to Smartsheet sheet with Multi-Select Dropdown values?


I'm building a webform that allows a user to create a new row in Smartsheet sheet. It's just a prototype at this stage but the one snag I'm running into is submitting the request where values are for a multi-select dropdown.

I've tried several things

#Attemp 1
new_cell.value = value
new_row.cells.append(new_cell)

#Attemp 2
multival = smart.models.MultiPicklistObjectValue()
multival.values = value
new_cell.object_value = multival
new_row.cells.append(new_cell)

#Attempt 3
multival = smart.models.MultiPicklistObjectValue()
multival.values = np.asarray(value)
new_cell.object_value = multival
new_row.cells.append(new_cell)

Then there's the errors...

#Attempt 1 error: {"response": {"statusCode": 400, "reason": "Bad Request", "content": {"detail": {"index": 0}, "errorCode": 1012, "message": "Required object attribute(s) are missing from your request: cell.value.", "refId": "nbw0ymylrr19"}}}

#Attempt 2 error: {"response": {"statusCode": 400, "reason": "Bad Request", "content": {"errorCode": 5536, "message": "The value "com.navigo.smartsheet.rest.helper.sheet.cell.model.MultiPicklist@31b27800" could not be saved in column "Audience". This column is restricted to PICKLIST values only.", "refId": "71du6l0civ0y"}}}

#Attempt 3 error: ValueError: ("Can't load to TypedList(%s) from '%s'", <class 'str'>, array(['Clinicians', 'Chest pain committee'], dtype='<U20'))

Attempt 3 doesn't even make it past the multival.values assignment.

I've seen a lot of documents on updating rows, querying Smartsheet but I don't see a lot in the ways of Multi-Select values or adding a new row. Has anyone solved this? Or does anyone have any idea what I'm doing wrong?


Solution

  • I'm not very familiar with the Smartsheet Python SDK, but it seems like the integration test code in the SDK's GitHub repo might be helpful. The test_multi_picklist.py file there contains this code that seems to match your scenario:

    def test_add_multi_picklist_row(self, smart_setup):
            smart = smart_setup['smart']
    
            mplov = smartsheet.models.MultiPicklistObjectValue({
                'values': ['Bat', 'Cat']
            })
    
            insert_cell = smartsheet.models.Cell({
                'column_id': smart_setup['multi_picklist_col_id'],
                'object_value': mplov
            })
    
            row = smart.models.Row({
                'to_top': True,
                'cells': [insert_cell]
            })
    
            action = smart.Sheets.add_rows(smart_setup['sheet'].id, [row])
            assert action.request_response.status_code == 200
    

    ** UPDATE - complete proof of concept **

    The following code uses the Smartsheet Python SDK to add a new row to a sheet (containing just 2 cells of data, for simplicity), where one of the cells belongs to a multi-select dropdown column in the sheet.

    # Initialize client. 
    # Uses the API token in the environment variable SMARTSHEET_ACCESS_TOKEN.
    smart = smartsheet.Smartsheet()
    
    # Define cell object (for the value in the first column) 
    cell1 = smart.models.Cell({
        'column_id': 6101753539127172,
        'object_value': 'new item'
    })
    
    # Define cell object (for the value in the second column - a multi-select dropdown) 
    cell2 = smart.models.Cell({
        'column_id': 8436269809198980,
        'object_value': smart.models.MultiPicklistObjectValue({'values': ['Blue', 'Green']})
    })
    
    # Create row that contains the 2 cells defined previously
    row = smart.models.Row({
        'cells': [cell1, cell2]
    })
    
    # Add row to sheet
    sheetId = 3932034054809476
    result = smart.Sheets.add_rows(sheetId, [row])
    

    Running this code successfully added the following row to my sheet (specifying 2 values for the multi-select dropdown column):

    enter image description here