1

I am working on Smartsheet API but I am not a pro. I need some help to get all the information out a specific sheet. With all information I mean all the columns and all attachments to them. I also need help to make it possible to download the attachments to the users device. Is there any one out there with a template in PHP? I have come so far to get the information from the columns out but it is not enough.

 $baseURL = "https://api.smartsheet.com/1.1";
$sheetsURL = $baseURL. "/sheets/";
$getSheetURL = $baseURL. "/sheet/xxx";
    $rowsURL = $baseURL. "/sheet/xxx/rows";
    $rowAttachmentsURL = $baseURL. "/sheet/3712544801089412/row/{{ROWID}}/attachments";
    $getAttachmentURL = $baseURL ."/sheet/3712544801089412/attachment/{{ATTACHMENTID}}";


    // Insert your Smartsheet API Token here
    $accessToken = "xxx"; 

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

            // Connect to Smartsheet API to get Selected Sheet
            $curlSession = curl_init($getSheetURL);

            curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
            $getSheetResponseData = curl_exec($curlSession);
            $sheetObj = json_decode($getSheetResponseData);


            //attachment


                echo "<h1>Sheet name: ". $sheetObj->name ."</h1>";
                echo "<br>";
                //table start
                echo "<table>";
                echo "<td>"."Attachment"."</td>";
                foreach ($sheetObj->columns as $column) {
                    echo "<td>".$column->title ."</td>";
                    } 

                    echo "<tr>";
                    foreach ($sheetObj->rows as $row) {

                        $curlSession = curl_init($rowAttachmentsURL);
                        curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
                        curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
                        $getAttachmentsResponse = curl_exec($curlSession);
                        // Assign response to variable 
                        $attachments = json_decode($getAttachmentsResponse);
                        $rowAttachmentsURL = str_replace('{{ROWID}}', $row->id, $rowAttachmentsURL);

                        var_dump($attachments);                             


                            foreach ($row->attachments as $attachment) {
                                echo "<td>".$attachment->name."</td>";
                                }    
                            foreach ($row->cells as $cell) 
                            {

                              echo "<td>".$cell->value."</td>";
                            }  

                            echo "<td></td><td></td><td></td>";
                            echo "</tr>"; 
                        }

                        echo "</table>";
                        curl_close($curlSession); 
                    ?>
Kim Brandl
  • 13,125
  • 2
  • 16
  • 21

1 Answers1

1

There is a project on GitHub that provides php sample code for working with the Smartsheet API. It sounds like you're looking to get all the columns in your sheet, as well as work with attachments.

To see how to get the contents of your sheet, including columns, I would recommend the HelloGetSheet sample. This sample gets a selected sheet and then outputs the columns and rows for that sheet.

Inside that same project, there is also sample code for working with Attachments. In addition to showing how to upload an attachment, this script shows how to retrieve and download attachments from your sheet.

UPDATE: Thanks for posting the code. You look like you have the main building blocks you need to accomplish what you're looking for. I would suggest moving your 'List Row Attachments' section inside your row loop. Then, for reach row you can grab the row ID and use that to get your attachments URL like so

$rowAttachmentsURL = str_replace('{{ROWID}}', $row->id, $rowAttachmentsURL);
stmcallister
  • 1,682
  • 1
  • 12
  • 21
  • The code i am using is the two from GitHub you mentioned. I have got the columns and rows out, but i can not combine them with the codes from the attachment file. so it would be great if you could give me an example of how to put the attachment code into the other codes so i can get all the information out of the smartsheet. Thanks. – user2979664 Feb 10 '15 at 20:01
  • @user2979664 could you post the code for what you have tried, and we can see where the trouble might be? – stmcallister Feb 10 '15 at 21:23
  • see the code in the top. My plan was to make a table with the attachment and other information just like smartsheet and then have a link one the attashment so there is a opportunity to download the file to any device. I do not need all the stuff with add new sheet, upload and so on. – user2979664 Feb 11 '15 at 09:33
  • @user2979664 Thanks for posting the code. I'll add some suggestions to my answer. In the mean time, you'll want to revoke the access token that you posted, and generate a new one. – stmcallister Feb 11 '15 at 18:03
  • Thanks for your help, but now i get this error: errorCode":1004,"message":"You are not authorized to perform this action. Why? – user2979664 Feb 11 '15 at 19:52
  • @user2979664 You'll want to check the value you're using for $accessToken, and make sure it matches the value of an active Access Token for your Smartsheet account. – stmcallister Feb 11 '15 at 22:35
  • Hi again. I am really sorry but i can get the attachment name and id out. I add my new code, would you pleace look at it and write the code as it should be? I really appreciate your help – user2979664 Feb 12 '15 at 13:07