[英]Python using Smartsheet API to update Cell value without SDK
[英]Some simple examples of Smartsheet API using the Python SDK
我是Smartsheet Python SDK的新手。 使用Smartsheets API doc中的示例代碼作為起點:
action = smartsheet.Sheets.list_sheets(include_all=True)
sheets = action.data
此代碼返回響應就好了。
我現在正在尋找一些簡單的例子來迭代表格,即:
for sheet in sheets:
然后按名稱選擇工作表
然后迭代選定工作表中的行並選擇一行。
for row in rows:
然后從所選工作表中的選定行檢索單元格值。
我只需要一些簡單的樣本即可開始使用。 我搜索了很多,無法找到任何簡單的例子,如何做到這一點謝謝!
正如Scott所說,工作表可能會返回大量數據,因此請確保您明智地使用過濾器。 下面是我編寫的一些代碼示例,用於拉出兩行,但每行只有一列:
action = smartsheet.Sheets.get_sheet(SHEET_ID, column_ids=COL_ID, row_numbers="2,4")
有關可用過濾器的詳細信息,請訪問此處 。
更新 :添加更多代碼以遵循網站禮儀並提供完整的答案。
我在學習API時做的第一件事是顯示我所有工作表及其相應的sheetId的列表。
action = MySS.Sheets.list_sheets(include_all=True)
for single_sheet in action.data:
print single_sheet.id, single_sheet.name
從該列表中我確定了要從中提取數據的工作表的sheetId。 在我的示例中,我實際上需要拉出主列,因此我使用此代碼來確定主列的Id(並且還將非主列Ids保存在列表中,因為當時我認為我可能需要它們) :
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)
最后,請記住檢索整個工作表可能會返回大量數據,我使用過濾器只返回主列中的數據:
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,但有一點不直觀的事情可能是從smartsheet.Sheets.list_sheets
返回的列表中的工作表對象不包含行和單元格。 由於這可能是大量數據,它會返回有關工作表的信息,您可以通過調用smartsheet.Sheets.get_sheet
來檢索工作表的完整數據。
為了更好地理解這樣的事情,請務必保持Smartsheet REST API參考 。 由於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
我開始使用SmartSheets的Python API。 由於我們使用智能表支持我們的一些RIO2016奧運會運營,我們不時為了許可證合規限制而不得不刪除最舊的Smartsheets。 這是一個大錯:登錄,選擇300個中的每個智能,檢查每個字段等等。 因此,感謝smartsheet API 2.0,我們可以輕松地了解到目前為止我們使用了多少張紙,獲得所有“修改”日期,按照從最新日期到最近日期的列進行排序,然后寫入CSV磁盤。 我不確定這是否是最佳方法,但它按預期工作。我使用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)
希望你喜歡。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.