5

I am hoping to be able to retrieve rows of data as a list from specific columns within C#. So if there was a column of person heights it would list those heights in a list. Potentially also list x,y values say number of apples on a particular date.

I have looked at the examples given on the API information and cannot find any examples on how to do this - they mostly consist of creating folders, users or listing folders or sheets or entering information on smartsheets etc, but none on actually getting data out.

Here is the code I have looked at: https://github.com/smartsheet-platform/samples/tree/master/c%23 https://github.com/smartsheet-platform/smartsheet-csharp-sdk

But I would actually like to pull the data out as a list and then process it a bit within C# for end users so I am not wanting to put it back into smartsheets.

Is the only way to do this to download file as Excel sheet using the API and going from there? I really wanted to skip this step if at all possible?

I should add I am wanting to use the C# SDK to do this.

The specific code I think I need to enter (I think) is this in order to get the sheet.

// Set the Access Token
Token token = new Token();
token.AccessToken = "INSERT_YOUR_TOKEN_HERE";
long id = "INSERT SHEET ID HERE";


// Use the Smartsheet Builder to create a Smartsheet
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(
token.AccessToken).Build();

//Code to get sheet
smartsheet.Sheets().GetSheet(long id, **IEnumerable<ObjectInclusion?includes**).Rows();

It's the last parameter I am not sure what they need. It says in the GetSheet method:

Sheet GetSheet( long id, IEnumerable includes )

Here is a link to the ObjectInclusion Enumeration - http://smartsheet-platform.github.io/smartsheet-csharp-sdk/html/T_Smartsheet_Api_Models_ObjectInclusion.htm

1 Answer 1

5

Here is an example which prints the cell data in every sheet.

        // Set the Access Token
        Token token = new Token();
        token.AccessToken = "YOUR_TOKEN";

        // Use the Smartsheet Builder to create a Smartsheet
        SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();

        // Gets just a list of sheets (not the actual data in the sheet)
        IList<Sheet> homeSheets = smartsheet.Sheets().ListSheets();
        foreach (Sheet tmpSheet in homeSheets)
        {
            Console.WriteLine("========== New Sheet: " + tmpSheet.Name);
            // Get the sheet with the data
            Sheet sheet = smartsheet.Sheets().GetSheet((long)tmpSheet.ID, new ObjectInclusion[] { ObjectInclusion.DATA, ObjectInclusion.COLUMNS });

            int rowCount = 0;
            foreach (Row tmpRow in sheet.Rows)
            {
                Console.Write(rowCount++ + ": ");
                foreach (Cell tmpCell in tmpRow.Cells)
                {
                    Console.Write(tmpCell.Value + "|");
                }
                Console.WriteLine();
            }
        }

To answer some of your other questions:

Is the only way to do this to download file as Excel sheet using the API and going from there? I really wanted to skip this step if at all possible?

The C# SDK and the API both support the ability to retrieve parts or all of the sheet data. You are not required to download the sheet as an Excel file in order to work with the data in the sheet.

I am not sure what they need. It says in the GetSheet method: Sheet GetSheet( long id, IEnumerable<ObjectInclusion>)

An IEnumerable is just a collection that is iterable. You can use any collection for the 2nd parameter that implements this interface. The collection should contain ObjectInclusion items in it's list. In my example I used an array as it implements an IList which implements an IEnumerable.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.