Creating a new sheet from another sheet's JSON

53 views Asked by At

Using the Smartsheet api (with Python3) If I get a sheet through the api with get_sheet() then use the .to_json() method of that sheet object, I can get a sheet object with sheet_spec = models.Sheet(data), but if I try to use that sheet_spec as the sheet object for create_sheet_in_folder() I get lots of "Field ... was of unexpected type" errors.

ex: smartsheet.exceptions.ApiError: {"result": {"code": 1008, "errorCode": 1008, "message": "Unable to parse request. The following error occurred: Field \"createdAt\" was of unexpected type.", "name": "ApiError", "recommendation": "Do not retry without fixing the problem. ", "refId": "avfbgy", "shouldRetry": false, "statusCode": 400}}

I assume this is because an existing sheet has some values that are auto-generated like the id field. If one wanted to make a new sheet from another sheet's json, converted to sheet object, what changes must be made to the sheet object?

This is probably not the standard way of doing this, but I can think of at least one situation in which it would be handy -- like using regex to find & replace a set of values on a sheet without having to know the column name or id - this could be done in one go on the raw json, then turn it back into a Sheet object and save as a new sheet.

Tried removing the createdAt field, but the create method throws an error on another field.

1

There are 1 answers

0
Kim Brandl On

I suspect you're correct regarding the reason for the error you're seeing -- the Get Sheet response will contain lots of properties that are read-only, which are set automatically when the sheet is created/updated. Attempting to specify any of these read-only properties in a Create Sheet request will result in an error.

Regardless of the reason for the error though -- I wouldn't recommend trying to create a copy of a sheet in the way you're attempting to. Creating a copy of a sheet can actually be a fairly complex process, as a sheet can contain not only data but also things like attachments, cellLinks, discussions, filters, sharing, etc. Sure, you could use the API to try to copy all of those objects to the new sheet, but what a painful process that would be. You might consider just using the Copy Sheet operation instead, which allows you to specify exactly what you want to copy over to the new sheet. (Although I understand this wouldn't satisfy your desire to use regex for find/replace in the raw JSON as you've described.)