【问题标题】:Retrieving SmartSheet Content using Python SDK使用 Python SDK 检索 SmartSheet 内容
【发布时间】:2016-08-10 07:52:53
【问题描述】:

所以我尽量保持简短。我是 SmartSheet Python SDK 的新手,我试图从 Smartsheet 中调用所有数据,并希望以此作为自己的起点。到目前为止我所拥有的

import smartsheet

smartsheet = smartsheet.Smartsheet('token1')

我在 Python Shell 上运行/编译,到目前为止,我正在运行这两条线并且没有遇到问题,当我尝试实现其他任何东西来提取数据时,我不断收到错误。

关于主题,但另一件事

我也有这行代码从 SmartSheet 中提取特定行,

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

我的问题是,我在哪里可以找到列 ID,我知道如何访问工作表 ID,但我无法访问或在智能表上找到列 ID,我不知道我是否忽略了它。只是想朝着正确的方向前进,作为我的新手,任何帮助表示赞赏。

编辑

5183127460046724
Task Name

2931327646361476
Duration

7434927273731972
Start

编辑 2

Task Name
task1 text here


Duration
duration1 text here

Start
start1 example here

【问题讨论】:

  • “当我尝试实施任何其他方法来提取数据时,我不断收到错误。” ——你能更新你的帖子吗?即,具体是哪一行代码导致错误,具体是您收到什么错误?关于如何获取列 ID,您可以通过 API 执行“获取列”操作(针对特定工作表),该操作的结果将包含该工作表中列的集合(每个列都有一个 id 属性)。 (“获取工作表”操作也会为您提供此信息 - 但如果您只对列感兴趣,那就太过分了。)
  • 当我尝试实现任何我认为会让我的程序检索信息的代码行时,这就是我的意思,没有什么特别的,但一般来说。我尝试了很多事情,不知道我是否朝着正确的方向前进。需要有关提取所有信息的指导,以便我可以开始学习如何编织我特别想要的任何数据
  • 我建议您花一些时间查看 Smartsheet API 文档:smartsheet-platform.github.io/api-docs/#python-sample-code。 “API 参考”部分中的主题包含每个操作的 Python 示例代码。例如,要在工作表中查找特定列的列 ID,您将使用“获取所有列”操作(此处的示例代码 >>smartsheet-platform.github.io/api-docs/?python#get-all-columns),然后检查结果以确定您的列的 ID重新感兴趣。
  • 感谢 Kim,所以我正在使用这些示例代码并运行它,但仍然出现错误。我正在使用这个:
  • 谢谢,所以我正在使用示例并使用我的凭据运行它,但我仍然收到错误 import smartsheet smartsheet = smartsheet.Smartsheet('token1') # all columns。 action = smartsheet.Sheets.get_columns(2596170863273860, include_all=True) columns = action.data action = smartsheet.Sheets.get_columns(sheetId) pages = action.total_pages columns = action.data 并获取此 Traceback(最近一次通话最后):文件“C:\Users\jxm10\Desktop\python1.py”,第 9 行,在 columns = action.data AttributeError: 'Error' object has no attribute 'data'

标签: python sdk smartsheet-api


【解决方案1】:

以下示例代码从指定的 Sheet 中检索所有列的列表,然后遍历列,打印出每列的 Id 和 Title。 (您显然需要将 ACCESS_TOKENSHEET_ID 替换为您自己的值。)

# 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('')

更新 #1

下面是一些示例代码,展示了如何在 Get Sheet 响应中访问属性值。

# 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('')

【讨论】:

  • 谢谢金!但是有一个问题,当我使用列的最后一点代码时,它会打印出如下结果: 5183127460046724 任务名称 2931327646361476 持续时间 7434927273731972 开始 我正在阅读文档,我不知道我是否错过了它提到打印出每列中的文本。不完全确定上面这些数字是从哪里得到的,或者它们的含义。附:我对原始帖子进行了编辑,以便您可以更好地看到它
  • 数字是列ID,每个数字后面的单词代表该列的标题。此操作(“列出所有列”)返回有关工作表中列的信息 - 它不会返回驻留在工作表单元格中的数据。要获得该信息,您需要使用“获取工作表”操作。跨度>
  • 嗨,Kim,谢谢,我做到了,似乎它带来了一张纸,还有更多,例如,{"format": null, "displayValue" : null, "linksOutToCells": 它看起来是 JSON 格式。有没有一种操作可以让我在不将其转换为 JSON 的情况下将其准确地打印在 smartsheets 上?
  • 或者给你一个视觉效果,例如我在原始帖子的第二次编辑中是如何得到它的
  • 或者至少我得到的东西类似于他们在示例代码的示例输出中所拥有的东西
猜你喜欢
  • 1970-01-01
  • 2023-02-17
  • 1970-01-01
  • 2018-05-06
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多