使用 Python SDK 的 Smartsheet API 的一些简单示例

Some simple examples of Smartsheet API using the Python SDK

我是 Smartsheet Python SDK 的新手。使用 Smartsheets API 文档中的示例代码作为起点:

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

这段代码returns一个响应就好了。

我现在正在寻找一些简单的示例来遍历 sheets 即:

for sheet in sheets:

然后select一个sheet名字

然后迭代 selected sheet 和 select 中的行。

for row in rows:

然后从 selected sheet 的 selected 行检索单元格值。

我只需要一些简单的示例即可开始。我进行了广泛的搜索,但找不到有关如何执行此操作的任何简单示例 谢谢!

下面是一个非常简单的例子。其中大部分是标准 python,但有一点不直观的是,从 smartsheet.Sheets.list_sheets 返回的列表中的 sheet 对象不包含行和单元格.由于这可能是很多数据,它是关于 sheet 的 returns 信息,您可以通过调用 smartsheet.Sheets.get_sheet.[= 来检索 sheet 的完整数据。 14=]

为了更好地理解此类内容,请务必将 Smartsheet REST API reference 放在手边。由于 SDK 实际上只是在幕后调用此 API,因此您通常也可以通过查看该文档找到更多信息。

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

正如 Scott 所说,sheet 可以 return 大量数据,因此请务必明智地使用过滤器。这是我编写的一些代码示例,用于拉两行但每行只有一列:

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

可以找到有关可用过滤器的详细信息here

更新:添加了更多代码以遵守网站礼仪并提供完整答案。

学习 API 时我做的第一件事是显示我所有 sheet 及其对应的 sheetId 的列表。

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

从该列表中,我确定了 sheet 我想从中提取数据的 sheet 的 ID。在我的示例中,我实际上需要提取 primary 列,因此我使用此代码来确定主列的 ID(并将非主列 ID 保存在列表中,因为当时我想我可能需要它们):

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)

最后,请记住,检索整个 sheet 可能 return 很多 数据,我使用过滤器 return仅主列中的数据:

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

我开始使用 Python APIs 和 SmartSheets。由于我们使用 smartsheets 来支持我们的一些 RIO2016 奥运会运营,为了许可证合规性限制,我们有时不得不删除最旧的 Smartsheets。这是一个错误:登录,select 300 百个中的每个智能,检查每个字段等等。所以感谢 smartsheet API 2.0,我们可以很容易地了解到目前为止我们已经使用了多少张纸,获取所有 'modified' 日期,按该列从最新到最近的日期排序,然后写入CSV 磁盘。我不确定这是否是最好的方法,但它有效,因为我 expected.I 使用 Idle-python2.7,Debian 8.5。你在这里:

    # -*- 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)

希望你喜欢。

问候