|
Los tipos de archivo clásicos dejarán de estar disponibles a partir de enero de 2021. Puedes migrar tus archivos clásicos o descargar un PDF. Más información

Writing data to a specific spreadsheet and sheet id

0

Comentarios

4 comentarios

  • Jeff Hickey

    Hi Hector Silva,

    It looks like you're attempting to use the Spreadsheet API Write Data to a Region endpoint. If this is a new build, I would encourage you to use the Platform API Spreadsheets Update Values in a Range endpoint (or the Prototype Platform API Content Tables Initiate Edits to Table Cells endpoint).

    Here are a few initial troubleshooting steps:

    1. Make sure you are using the correct base URL. Right now, you've configured your API call for US. More information can be found here https://developers.workiva.com/spreadsheets-v1/spreadsheets-spreadsheets-details/
    2. /data/ is missing from the endpoint path. See below for full required path
    3. Your call is missing the required Region path parameter. The full endpoint path should look like this {baseURL}/spreadsheets/{SpreadsheetId}/sheets/{SheetId}/data/{Region}
    4. Verify that the API Grant you're using was created on the same Workspace that the Spreadsheet is on
    5. Verify that the Workiva Username associated to the API Grant has explicit edit permissions to the Sheet
    6. Recommended that you migrate the call to the Platform or Prototype Platform API endpoints rather than the Spreadsheet API as this API has been deprecated

    After making any necessary changes above, if you're still running into errors, could you please post the full request you're sending?

    0
  • Hector Silva

    Thank you for the information.  I'm still encountering the same error, after updating as suggested.

    I added diagnostics and can see what I am successfully connecting and retrieving the necessary permissions, but still can't seem to crack the nut.  Here is my full request:

    def write_data_to_workiva(self, r):


            """Writes a pandas DataFrame to a Workiva spreadsheet."""

            


            spreadsheet_id = r.spreadsheet_id


            sheet_id = r.sheet_id


            region = ":"

            

            


            self.logger.info(f"Writing to spreadsheet_id: {spreadsheet_id}, sheet_id: {sheet_id}")


            self.logger.info(f"Data shape: {r.sps_input.shape}")

            


            # Convert DataFrame to list of lists


            if r.sps_input.empty:


                self.logger.warning("No data to write - DataFrame is empty")


                return

                


            data_to_write = [r.sps_input.columns.tolist()] + r.sps_input.values.tolist()

            


            # Use correct Workiva API with range


            url = f"https://api.app.wdesk.com/platform/v1/spreadsheets/{spreadsheet_id}/sheets/{sheet_id}/data/{region}"

            


            headers = {


                "Content-Type": "application/json",


                "Accept": "application/json",


                "Authorization": f"Bearer {r.bearer_token}"

            }

            


            payload = {


                "values": data_to_write

            }

            


            try:


                self.logger.info(f"Writing to: {url}")


                response = requests.put(url, headers=headers, json=payload)


                if response.status_code in [200, 201]:


                    self.logger.info("Data successfully written to Workiva spreadsheet.")


                else:


                    self.logger.error(f"Failed with {response.status_code}: {response.text}")


            except Exception as e:


                self.logger.error(f"Error writing to Workiva: {e}")

    0
  • Hector Silva

    To be more specific, the spreadsheet and sheet id are entered as parameters by users.

    0
  • Jeff Hickey

    Hi Hector Silva

    If you are switching to the Platform API, it has a different URL structure than the deprecate Spreadsheets API. The path you've configured is incorrect for the Update Values in a Range endpoint. Below is a sample starting point that assumes you are operating in the US environment. If you are not, see API Details for your correct base URL. You'll also need to apply numbers 4 and 5 above for each Spreadsheet and Sheet pair.

    import requests

    url = "https://api.app.wdesk.com/platform/v1/spreadsheets/{spreadsheetId}/sheets/{sheetId}/values/{range}"

    payload = { "values": [[1, 4], [2, ""]] }

    headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {access-token}' } r = requests.put(url, json=payload, headers = headers) print(r.headers['Location'])

    Required Platform API Update Values in a Range documentation: https://developers.workiva.com/platform-v1/platform-getvaluesbyrange/

    0

Iniciar sesión para dejar un comentario.