Tun Tun
Tun Tun

Reputation: 41

Update Cell SmartSheet 2.0 with PHP Script

I have found this piece of code to update cell in SmartSheet website. But I I don't understand what are the values in the fields variable. Is anyone has the working example of PHP code to do this?

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/rows?include=objectValue \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-X PUT \
-d '[{"id": "6572427401553796", "cells": [{"columnId": 7518312134403972,"objectValue": {"objectType": "PREDECESSOR_LIST","predecessors": [{"rowId": 567735454328708,"type": "FS","lag": {"objectType": "DURATION","days": 2,"hours": 4}}]}}]}]'

My current php code is as follow and I just want to update some value into the sheet.I don't know what to put in the field varaible. Thanks.

<?php

$baseURL = "https://api.smartsheet.com/2.0";
$getSheetURL = $baseURL. "/sheets/4925037959505xxx/rows?include=objectValue";
$accessToken = "34ouqtkxp0sutdv6tjbwtsxxxx"; 

$headers = array("Authorization: Bearer ". $accessToken ,  "Content-Type: application/json");


$fields='[????]';

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

Thanks

Upvotes: 0

Views: 758

Answers (2)

Paul S. Chase
Paul S. Chase

Reputation: 11

This is a function I created to update a SmartSheet

  • $ssID: is the Smart Sheet ID
  • $ssRowID: is the Smart Sheet row id I want to update
  • $values: is an array of column values
  • $columArray: is an array of the column IDs
  • $config: is an array I use to hold all auth information

Breaking down the Json Data which is the way you communicate which columns you want to update and the values

  • {"id": "'.$ssRowID.'",

    • "cells":

    • [

    • {"columnId": '.$columnArray['go'].',"value": "'.$values['go'].'"},
    • {"columnId": '.$columnArray['comments'].',"value": "'.$values['comment'].'"},
    • {"columnId": '.$columnArray['data center'].',"value": "'.$values['datacenter'].'"}
    • ]
  • }

  • ID represents the row you want to update

  • ColumnID represents each column you want to update must provide the column ID and value represents the value you want to insert

{"columnId": 'your column id here',"value": 'your value here'}

function ssUpdate($ssID, $ssRowID, $values, $columnArray, $config){


    /*
    *   update the Smart Sheet using the passed in sheet ID, Row ID and VALUES
    *   
    */

    $sheetID = $ssID; 
    $SSAPIToken = $config['smartsheets']['SSAPIToken'];
    $sheetsURL = "https://api.smartsheet.com/2.0/sheets/".$sheetID."/rows";
    $data_json = '[{"id": "'.$ssRowID.'", "cells": [{"columnId": '.$columnArray['go'].',"value": "'.$values['go'].'"}, {"columnId": '.$columnArray['comments'].',"value": "'.$values['comment'].'"}, {"columnId": '.$columnArray['data center'].',"value": "'.$values['datacenter'].'"}]}]';

    //echo $data_json."<br />";

    // Create Headers Array for Curl
    $headers = array(
        "Authorization: Bearer " .$SSAPIToken,
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_json)
    );

    /*
    *   connect to SS and update Sheet
    */

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$sheetsURL ); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);

    $data = curl_exec($ch); 
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    $information = curl_getinfo($ch);

    curl_close($ch);

    $ssArray = json_decode ($data, true); // make an XML object

    /*
    *   did connection work
    */

    if ($status_code != 200) {
        echo "Oh No! Update Error: (". $ssArray['errorCode'] .") ". $ssArray['message'] ."\n";
    } else {

        //var_dump($ssArray);

        if ($ssArray['resultCode'] == 0){
            echo "Updated: ".$values['store'];
        }else{
            echo "Oh No! Update Error: (". $ssArray['errorCode'] .") ". $ssArray['message'] ."\n";
        }   

    }

}

More details: http://smartsheet-platform.github.io/api-docs/#add-row-s

Upvotes: 1

Steve Weil
Steve Weil

Reputation: 873

$fields is the JSON payload for the PUT request. Details are documented here: http://smartsheet-platform.github.io/api-docs/#update-row-s

I suggest you test using Postman or the Advanced Rest Client before you begin coding.

Also, include=objectValue only makes sense for a GET so there is no point including it for a PUT request.

Upvotes: 0

Related Questions