如何在Smartsheet Python SDK中使用搜索结果?

2024-10-03 11:14:23 发布

您现在位置:Python中文网/ 问答频道 /正文

启动程序后

results = smart.Search.search("2244113312180")
print(results)

获取数据

{"results": 
[{"contextData": ["2244113312180"], 
"objectId": 778251154810756, 
"objectType": "row", 
"parentObjectId": 3648397300262788,
 "parentObjectName": "Sample Sheet", 
"parentObjectType": "sheet", 
"text": "2244113312180"}, 
{"contextData": ["2244113312180"],
 "objectId": 7803446734415748, 
"objectType": "row", 
"parentObjectId": 3648397300262788, 
"parentObjectName": "Sample Sheet", 
"parentObjectType": "sheet",
"text": "2244113312180"}], 
"totalCount": 2}

如何在程序中正确使用它们? 请提供一个正确的用法示例

以及如何找到在其中找到值“2244113312180”的id_列

new_row = smartsheet.models.Row()

new_row.id = results.objectId

对不起,我没有立即写下错误。我无法使用结果中的属性。字符串:

new_row.id = results.objectId

引起错误

AttributeError: 'SearchResult' object has no attribute 'objectId'

谢谢你的帮助

顺便说一下,我找到了方法

results = smart.Search.search("2244113312180")
text = str(results)
json_op = json.loads(text)
for i in json_op["results"]:
    new_row = smartsheet.models.Row()
    new_row.id = i["objectId"]

我不知道这是不是一个好的解决方案


Tags: text程序idjsonnewsearchsmartresults
1条回答
网友
1楼 · 发布于 2024-10-03 11:14:23

根据Smartsheet API文档中的SearchResultItem Object定义,搜索结果项永远不会包含关于存在值的列的信息。正如您发布的JSON结果所示,如果在工作表的行中(即,在该行包含的任何单元格中)找到指定的值,则相应的搜索结果项将标识工作表ID(parentObjectId)和行ID(objectId

然后可以使用这两个值检索行,如文档的Get Row部分所述:

row = smartsheet_client.Sheets.get_row(
  4583173393803140,       # sheet_id 
  2361756178769796       # row_id 
)

然后可以遍历row.cells数组,检查每个单元格的value属性,以确定它是否与之前搜索的值匹配。当您找到包含该值的cell对象时,该cell对象的column_id属性将为您提供匹配值所在的列ID

更新:

感谢您在原始帖子中澄清信息。我正在更新这个答案,以提供实现我前面描述的方法的完整代码示例。希望这是有帮助的

此代码示例执行以下操作:

  • 搜索Smartsheet中的所有内容(正在使用的API令牌的持有者有权访问)以查找字符串值
  • 迭代搜索结果项以处理任何“行”结果(即字符串出现在工作表单元格中的任何位置)
  • 用字符串new value替换工作表(单元格)中出现的任何内容
# set search criteria
query = '2244113312180'

# search everything 
search_results = smart.Search.search(query)

# loop through results 
# (acting upon only search results that appear within a row of a sheet)
for item in search_results.results:
    if item.object_type == 'row':
        # get row
        row = smart.Sheets.get_row(
            item.parent_object_id,       # sheet_id 
            item.object_id               # row_id
        )

        # find the cell that contains the value and update that cell value 
        for cell in row.cells:
            if cell.value == query:
                # build new cell value
                new_cell = smartsheet.models.Cell()
                new_cell.column_id = cell.column_id
                new_cell.value = "new value"
                new_cell.strict = False

                # build the row to update
                new_row = smartsheet.models.Row()
                new_row.id = item.object_id
                new_row.cells.append(new_cell)

                # update row
                result = smart.Sheets.update_rows(
                    item.parent_object_id,      # sheet_id
                    [new_row])

相关问题 更多 >