简体   繁体   中英

Some simple examples of Smartsheet API using the Python SDK

I am newbie to the Smartsheet Python SDK. Using the sample code from the Smartsheets API doc as a starting point:

action = smartsheet.Sheets.list_sheets(include_all=True)
sheets = action.data

This code returns a response just fine.

I am now looking for some simple examples to iterate over the sheets ie:

for sheet in sheets:

then select a sheet by name

then iterate over the rows in the selected sheet and select a row.

for row in rows:

then retrieve a cell value from the selected row in the selected sheet.

I just need some simple samples to get started. I have searched far and wide and unable to find any simple examples of how to do this Thanks!

As Scott said, a sheet could return a lot of data, so make sure that you use filters judiciously. Here is an example of some code I wrote to pull two rows but only one column in each row:

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

Details on the available filters can be found here .

UPDATE : more code added in order to follow site etiquette and provide a complete answer.

The first thing I did while learning the API is display a list of all my sheets and their corresponding sheetId.

action = MySS.Sheets.list_sheets(include_all=True)
for single_sheet in action.data:
    print single_sheet.id, single_sheet.name

From that list I determined the sheetId for the sheet I want to pull data from. In my example, I actually needed to pull the primary column, so I used this code to determine the Id of the primary column (and also saved the non-primary column Ids in a list because at the time I thought I might need them):

PrimaryCol = 0
NonPrimaryCol = []
MyColumns = MySS.Sheets.get_columns(SHEET_ID)
for MyCol in MyColumns.data:
    if MyCol.primary:
        print "Found primary column", MyCol.id
        PrimaryCol = MyCol.id
    else:
        NonPrimaryCol.append(MyCol.id)

Lastly, keeping in mind that retrieving an entire sheet could return a lot of data, I used a filter to return only the data in the primary column:

MySheet = MySS.Sheets.get_sheet(SHEET_ID, column_ids=PrimaryCol)
for MyRow in MySheet.rows:
    for MyCell in MyRow.cells:
        print MyRow.id, MyCell.value

Below is a very simple example. Most of this is standard python, but one somewhat non-intuitive thing about this may be the fact that the sheet objects in the list returned from smartsheet.Sheets.list_sheets doesn't include the rows & cells. As this could be a lot of data, it returns information about the sheet, that you can use to retrieve the sheet's complete data by calling smartsheet.Sheets.get_sheet .

To better understand things such as this, be sure to keep the Smartsheet REST API reference handy. Since the SDK is really just calling this API under the covers, you can often find more information by look at that documentation as well.

action = smartsheet.Sheets.list_sheets(include_all=True)
sheets = action.data
for sheetInfo in sheets:
    if sheetInfo.name=='WIP':
        sheet = smartsheet.Sheets.get_sheet(sheetInfo.id)
        for row in sheet.rows:
            if row.row_number==2:
                for c in range(0, len(sheet.columns)):
                    print row.cells[c].value

I started working with Python APIs with SmartSheets. Due to our usage of smartsheets to back some of our RIO2016 Olympic Games operations, every now and then we had to delete the oldest Smartsheets for the sake of licence compliance limits. And that was a blunder: login, select each smarts among 300 hundred, check every field and so on. So thanks smartsheet API 2.0, we could learn easily how many sheets we have been used so far, get all the 'modified' date, sort by that column from the latest to the most recent date and then write to a CSV disk. I am not sure if this is the best approach for that but it worked as I expected.I use Idle-python2.7, Debian 8.5. Here you are:

    # -*- coding: utf-8 -*-
    #!/usr/bin/python

    '''
    create instance of Sheet Object.
    Then populate List of Sheet Object with name and modified
    A token is necessary to access Smartsheets
    We create and return a list of all objects with fields aforesaid.
    '''

    # The Library

    import smartsheet, csv

    '''
    Token long var. This token can be obtained in 
    Account->Settings->Apps...->API
    from a valid SmartSheet Account.
    '''
    xMytoken=xxxxxxxxxxxxxxxxxxxxxx 

    # Smartsheet Token
    xSheet = smartsheet.Smartsheet(xMyToken)

    # Class object
    xResult = xSheet.Sheets.list_sheets(include_all=True)

    # The list 
    xList = []

    '''
    for each sheet element, we choose two, namely name and date of modification. As most of our vocabulary has special characters, we use utf-8 after the name of each spreadsheet.So for each sheet read from Object sheets
    '''
    for sheet1 in xResult.data.    
        xList.append((sheet1._name.encode('utf-8'),sheet1._modified_at))

    # sort the list created by 'Modifiedat' attribute
    xNlist = sorted(xList,key=lambda x: x[1])

    # print list
    for key, value in xNlist:
        print key,value

    # Finally write to disk    

    with open("listofsmartsh.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(xNList)

Hope you enjoy.

regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM