Search code examples
pythonsmartsheet-api

Smartsheet API Token Python search query


How can I get the rows based on column value? In simple words, how can we write a search query and get the rows from smartsheet using python sdk. Like

get rows where status = "Complete"


Solution

  • There's no single action that will let you query Smartsheet in the manner you've described.

    The following code will accomplish your goal, but please note: I'm no Python expert, so I imagine there's a much faster way to accomplish this in Python -- e.g., perhaps even by using a single statement to filter the collection of columns and then rows (instead of iterating through them like this code does).

    sheetId = 3932034054809476
    
    # get the sheet
    sheet = smartsheet_client.Sheets.get_sheet(sheetId) 
    
    # identify the column ID of the column you're wanting to query/filter by
    col_id = 0
    for col in sheet.columns:
        if col.title == 'Status':
            col_id = col.id
            break
    
    # iterate through rows creating a collection of Rows that match specified criteria
    rows = []
    for row in sheet.rows:
        for cell in row.cells:
            if cell.column_id == col_id and cell.value == 'Complete':
                rows.append(row)
    
    # print number of rows that matched criteria
    print(str(len(rows)) + ' matched the specified criteria.')
    print('')
    
    # print json for matching rows
    print('Here\'s the JSON for the matching rows:')
    for row in rows:
        print(json.dumps(row.to_dict()))
        print('')