Search code examples
pythongoogle-apis-explorerpygsheets

gsheets inserting python dictonary using batchupdate


I am new to gheets and am looking for a way to insert the data that I have scraped into gheets that is currently in a python dictionary. I would like to do this in a batch process so my spider can update multiple fields at once.

{'page_url': 'https://www.websiteurl.com', 'company': 'my company', 'location': 'The Netherlands',
                  'price_excl_vat': '30000'}

I tried to build a json request but am getting TypeError: unhashable type: 'dict'. I know its because the data field takes a string but thought qould be able to take a dictionary and cant seem to find how to do thie. Been at this all day and just cant seem to figure out what I am doing wrong.

batch_update_spreadsheet_request_body =  {

    {
     "requests": [
      {
       "insertRange": {
        "range": {
         "sheetId": '12345tutu',

        },
       }
      },
      {
       "pasteData": {
        "data":  {'page_url': 'https://www.websiteurl.com', 'company': 'my company', 'location': 'The Netherlands',
                  'price_excl_vat': '30000'},

       }
      }
     ]
    }

}

request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=batch_update_spreadsheet_request_body)
response = request.execute()

Solution

  • You should remove a pair of {}:

    batch_update_spreadsheet_request_body = {
         "requests": [
          {
           "insertRange": {
            "range": {
             "sheetId": '12345tutu',
    
            },
           }
          },
          {
           "pasteData": {
            "data":  {'page_url': 'https://www.websiteurl.com', 'company': 'my company', 'location': 'The Netherlands',
                      'price_excl_vat': '30000'},
    
           }
          }
         ]
        }
    

    It creates a set containing a dictionnary, and that is not possible, because as the error says, sets elements have to be hashable, and a dict is not.