Search code examples
pythonsdksmartsheet-api

Retrieving SmartSheet Content using Python SDK


so ill try to keep this short as possible. I am a newbie to the SmartSheet Python SDK and im trying to call all the data from the Smartsheet and hopefully use that as a starting point for myself. So far what I have

import smartsheet

smartsheet = smartsheet.Smartsheet('token1')

Which I am running/compile on Python Shell, and so far with those two lines I am running it and not getting an issue, when I try to implement anything else to pull the data, I keep getting errors.

on topic but a separate thing

I also have this line of code to pull specific line from the SmartSheet,

action = smartsheet.Sheets.get_sheet(SheetID, column_ids=COL_ID, row_numbers="2,4")

My question regarding this is, where do I find the Column ID, I know how to access the Sheet ID, but I cant access or find the Column ID on smartsheets, I dont know if i am overlooking it. Just looking to get off in the right direction, being the newbie I am, any help appreciated.

EDIT

5183127460046724
Task Name

2931327646361476
Duration

7434927273731972
Start

EDIT 2

Task Name
task1 text here


Duration
duration1 text here

Start
start1 example here

Solution

  • The following sample code retrieves a list of all columns from the specified Sheet, and then iterates through the columns printing out the Id and Title of each column. (You'll obviously need to replace ACCESS_TOKEN and SHEET_ID with your own values.)

    # Import.
    import smartsheet
    
    # Instantiate smartsheet and specify access token value.
    smartsheet = smartsheet.Smartsheet('ACCESS_TOKEN')
    
    # Get all columns.
    action = smartsheet.Sheets.get_columns(SHEET_ID, include_all=True)
    columns = action.data
    
    # For each column, print Id and Title.
    for col in columns:
        print(col.id)
        print(col.title)
        print('')
    

    UPDATE #1

    Here's some sample code that shows how to access property values within a Get Sheet response.

    # Get Sheet - but restrict results to just 2 rows (#2, #4) and a single column/cell (Id = COLUMN_ID)
    action = smartsheet.Sheets.get_sheet(SHEET_ID, column_ids=COLUMN_ID, row_numbers="2,4") 
    
    # print info from row #2 (the first row in the "Get Sheet" response) for the single specified column (cell)
    print('Row #: ' + str(action.rows[0].row_number))
    print('Row ID: ' + str(action.rows[0].id))
    print('Column ID: ' + str(action.columns[0].id))
    print('Column Title: ' + action.columns[0].title)
    print('Cell (display) value: ' + action.rows[0].cells[0].display_value)
    print('')
    
    # print info from row #4 (the second row in the "Get Sheet" response) for the single specified column (cell)
    print('Row #: ' + str(action.rows[1].row_number))
    print('Row ID: ' + str(action.rows[1].id))
    print('Column ID: ' + str(action.columns[0].id))
    print('Column Title: ' + action.columns[0].title)
    print('Cell (display) value: ' + action.rows[1].cells[0].display_value)
    print('')