Search code examples
python-3.xsmartsheet-api

Error's when posting data to smartsheets columns via api (python)


So I am currently attempting to post data through the smarthsheets api. My data is stored in a list of tuples such as this for an example:

[('hello','hi','description here'), ('I', 'Appreciate','the','help')] <- this is obviously for reference but just want to clarify that all the data in the tuples are strings. Now when I go to post the data that I have to smartsheets I get the following error,

{"response": {"statusCode": 404, "reason": "Not Found", "content": {"errorCode": 1006, "message": "Not Found", "refId": "ra7osyecdyx8"}}}
{"result": {"code": 1006, "errorCode": 1006, "message": "Not Found", "name": "ApiError", "recommendation": "Do not retry without fixing the problem. ", "refId": "ra7osyecdyx8", "shouldRetry": false, "statusCode": 404}}

The code that I have that I'm using to get the sheetid of the sheet in smartsheets and then add to the column is as

def getColumns(sheetId, columnList):
    try:
        columnList = []
        action = smart.Sheets.get_columns(
            sheetId, include_all=True)
        columns = action.data
        for col in columns:
            columnList.append(col.id)
        return columnList
    except Exception as e:
        time.sleep(30)
        print(traceback.format_exc())
        print(e)

def addToColumns(columnId, sheetId, columnData):
    try:
        column_spec = smartsheet.models.Column({
            'title': columnData,
            'type': 'TEXT_NUMBER',
            'options': ["One"],
            'index': 0
            })
        
        response = smart.Sheets.update_column(
            columnId,
            sheetId,
            column_spec

        )
        print(response)
    except ValueError as e:
        # time.sleep(30)
        # print(traceback.format_exc())
        print(e)
    getColumns()

    return

These two functions are called in the main piece of code here:

try:

        response = requests.request(
            'GET', url, headers=headers, data=payload, params=params)
        data = response.json()
        cloudcheckrBillingData = []
        columnList = []
        sheetColumns = getColumns(cloudCheckrCurrentMonth, columnList)
        for x in data['BillByAccount']:
            # print(data['BillByAccount'])
            billData = x['Account'], x['MonthlyToDateBill'], x['MonthlyToDateCost'], x['MonthlyToDateCredits']
            cloudcheckrBillingData.append(billData)


        for r in sheetColumns:
            for i in cloudcheckrBillingData:
                for string in i:
                    addToColumns(r, cloudCheckrCurrentMonth, string)
                
        return 
    except Exception as e:
        print(e)

The goal I am trying to reach is to take the data that I visualized above that is stored in the "cloudcheckrBillingData" list and then iterate over the column id's and the tuples to add the data appropriately in the addToColumns() function. The error happens in the addToColumns() function


Solution

  • I'm not 100% following the scenario you've described -- but here's some feedback that may be helpful.

    First, a 404 error in response to a Smartsheet API call most often means that (at least) one of the IDs that's specified in the URI either doesn't exist OR the user who issued the API call (i.e., the user account that owns the token that's being used to authenticate the API call) doesn't have access to the item. For example, if I use my API token to issue an Update Column request with URI PUT /sheets/123/columns/456, I'd receive a 404 response if any of the following are true:

    • No sheet having ID 123 exists.
    • Sheet having ID 123 exists, but it does not contain a column having ID 456.
    • Sheet having ID 123 exists, but I don't have adequate permissions to access it.

    Let's step back from the 404 error for a minute though, as I think there may be a larger issue. Do I understand correctly that you're trying to add new rows of data to the sheet? If so, then you'd want to use the Add Rows operation to do that. Your function addToColumns is calling the Update Column operation, which is intended to do things like update the properties of a column (data type, etc.), move a column, or rename a column. Based on the scenario you've described, that doesn't seem like the right operation.