更新智能表中的单元格

2024-01-09

我正在尝试使用 PHP 更新现有 SmartSheet 工作表中的一些单元格。添加新行没有问题,但我无法获取正确的 JSON 进行更新。

我此时的代码:

$ch = curl_init("https://api.smartsheet.com/1.1/sheet/1234567890/rows/");

$header = array("Authorization: Bearer xxxxxxxxxxx", 
            "Content-Type: application/json",
            "Assume-User: xxxx%40xxxx.com");
$name = 'MyName';
$fields =  '{"rowNumber":1, "columnId": 1234567890, "value": "'.$name.'", "displayValue": "'.$name.'"}';

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $fields);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);

这导致:

 "{"errorCode":1008,"message":"Unable to parse request. The following error occurred: Unknown attribute \"columnId\" found at line 1, column 45"}"

我尝试了很多选项,但无法通过 API 文档解决这个问题,也找不到任何其他执行相同操作的 PHP 示例。有人知道如何连续更新一个特定单元格吗?


The Docs

可以找到更新行的API文档here http://www.smartsheet.com/developers/api-documentation#h.iivq0zgz3g8q。它给出了从命令行使用curl的以下示例:

curl https://api.smartsheet.com/1.1/row/{rowId}/cells \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-X PUT \
-d '[ {"columnId": 3738748463671172, "value": "Revision 2"}, {"columnId": 5427598323935108, "value": "On Time", "strict": false} ]'

PHP 示例中需要更改的内容

我们需要更改您的 PHP 示例以按照上述文档发送数据。因此,需要更改 php 示例中的以下几项:

  1. URL 的格式必须为 https://api.smartsheet.com/1.1/row/{rowId}/cells,其中 {rowId} 替换为实际行 ID。
  2. 我们需要发送一组单元格。例如,以下是两个单元格的数组:[{"columnId": 13214124123213, "value": "my new text1"}, {"columnId": 1231231241238, "value": "my new text2"}]
  3. 我们需要将数据作为 PUT 请求发送。

解决方案

考虑到这一点,我们可以将代码更改为以下内容:

<?php
$ch = curl_init("https://api.smartsheet.com/1.1/row/4407426335172484/cells");

$header = array("Authorization: Bearer 1238123lkjafasdilfasflkj", 
            "Content-Type: application/json",
            "Assume-User: some%40email.com");
$name = 'MyName';
$fields =  '[{"columnId": 4431344890603396, "value": "'.$name.'", "displayValue": "'.$name.'"}]';

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $fields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$result = curl_exec($ch);
print_r($result);
?>

额外的信息

您可能想知道 rowId 和 columnId 从哪里来。这两个 ID 需要通过获取工作表从 API 中检索。这可以使用 Curl 来完成,适用于 Chrome 的高级 Rest 客户端 https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US或通过 PHP。 PHP 示例如下:

<?php
$ch = curl_init("https://api.smartsheet.com/1.1/sheet/1837937135511428");

$header = array("Authorization: Bearer 123l1k2j321lkjasdfa", 
            "Content-Type: application/json",
            "Assume-User: some%40email.com");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
print_r($result);
?>

此请求将输出类似以下内容并列出列 id 和行 id。

{
   "id":1837937135511428,
   "name":"test",
   "version":3,
   "columns":[
      {
         "id":4431344890603396,
         "index":0,
         "title":"Primary Column",
         "type":"TEXT_NUMBER",
         "primary":true,
         "width":150
      },
      {
         "id":8934944517973892,
         "index":1,
         "title":"Column2",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {
         "id":138851495765892,
         "index":2,
         "title":"Column3",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {
         "id":4642451123136388,
         "index":3,
         "title":"Column4",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {
         "id":2390651309451140,
         "index":4,
         "title":"Column5",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {
         "id":6894250936821636,
         "index":5,
         "title":"Column6",
         "type":"TEXT_NUMBER",
         "width":150
      }
   ],
   "rows":[
      {
         "id":4407426335172484,
         "rowNumber":1,
         "cells":[
            {
               "columnId":4431344890603396,
               "type":"TEXT_NUMBER",
               "value":"My Name",
               "displayValue":"My Name"
            },
            {
               "columnId":8934944517973892,
               "type":"TEXT_NUMBER",
               "value":"test",
               "displayValue":"test"
            }
         ],
         "expanded":true,
         "createdAt":"2014-06-23T09:32:02-07:00",
         "modifiedAt":"2014-06-23T09:48:55-07:00"
      }
   ],
   "accessLevel":"OWNER",
   "ganttEnabled":false,
   "dependenciesEnabled":false,
   "permalink":"https://app.smartsheet.com/b/home?lx=v75AYzRUICSXF_2oV6V_LA",
   "workspace":{
      "id":6724185599829892,
      "name":"Test"
   },
   "createdAt":"2014-06-23T09:28:58-07:00",
   "modifiedAt":"2014-06-23T09:48:55-07:00"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新智能表中的单元格 的相关文章

随机推荐