无法使用 Smartsheet 更新行 API

cannot update row using Smartsheet API

我几乎一字不差地使用 http://smartsheet-platform.github.io/api-docs/?python#update-row(s) 中的 Python 示例。区别在于我只更新一行中的一个单元格。我可以看到变量 row_a 中的值发生变化,但该行未在 Smartsheet 本身中更新。

这是我的代码,与 API 指南中发布的代码几乎相同:

row_a = smartsheet.Sheets.get_row(sheetId, 916467282667396)
cell_a = row_a.get_column(5937660066850692)
cell_a.value = 'new value'
row_a.set_column(cell_a.column_id, cell_a)
smartsheet.Sheets.update_rows(sheetId, [row_a])

看到Smartsheet中运行这段代码后没有出现文字'new value',所以在最后一行前面加上了print字样查看 API 调用返回的内容,这是结果(为了便于阅读,我添加了缩进):

{
    "requestResponse": null, 
    "result": {
        "code": 1062, 
        "name": "InvalidRowLocationError", 
        "recommendation": "Do not retry without fixing the problem.", 
        "shouldRetry": false, 
        "message": "Invalid row location.", 
        "statusCode": 400
    }
}

如何修复 InvalidRowLocationError 并将我的行更新发送到 Smartsheet?

smartsheet-python-sdk 1.0.1 版中实际上有两个与 update_rows 相关的错误,但也有一个解决方法。如果您尝试更新单元格缩进的行,则会遇到错误 InvalidRowLocationError(请参阅 https://github.com/smartsheet-platform/smartsheet-python-sdk/issues/44). The error NotEditableViaApiError is encountered if you try to update a row where a cell contains formulas, links to other cells, system values, or Gantt values (see bug description at https://github.com/smartsheet-platform/smartsheet-python-sdk/issues/42 处的错误描述)。

无论您尝试更新行中的哪个单元格,都会发生这些错误,因为 smartsheet-python-sdk 会更新整行。请务必注意 API 有效。所以解决方法是使用 Python requests 模块来执行实际更新,如下所示:

import requests
url = "https://api.smartsheet.com/2.0/sheets/SHEETID/rows"
payload = "{\"id\": 6436521654937476, \"cells\": [{\"columnId\": 8276294740797316,\"value\": \"new value\"}]}" 
# headers omitted from here for privacy
headers = { YYYYYYYYYYYYYYYYYYYYY }
response = requests.request("PUT", url, data=payload, headers=headers)
print(response.text)

在上面的示例中,有效负载仅包含我要更新的一个单元格,而不是整行。一个更具可读性的有效负载版本是(只有一个单元格的行对象):

{
  "id": 6436521654937476,
  "cells": [
    {
      "columnId": 8276294740797316,
      "value": "new value"
    }
  ]
}

这里的实际问题是请求要求您使用 PUT 而不是 POST。 参见:http://smartsheet-platform.github.io/api-docs/?shell#update-row(s)

我找到了一种仍然使用 sdk 的方法,同时只更新行中的某些单元格,其中给定行具有公式和阻止与 NotEditableViaApiError 相关的 api 更新的内容。

for row in sheet.rows:
    cell = row.get_column(column_id)
    cell.value = 'modified cell value'  # edit the cell locally

    # gather more cells and add to list of cells for row...

    # remove all cells in row and just add back the single cell or list of cells
    row.cells = [cell]
    print my_sheets.update_rows(sheet_id, [row])  # update sheet

每行仍有一个 api 调用让我能够正常工作,但至少有一种方法可以解决只更新给定行的某些单元格的问题。此调用将仅更新行列表中的单元格,其他单元格保持不变。