0

I am trying to analyse data from a google sheet using pygsheets wrapper and able to draw a line chart using same. The idea is to run data analysis on daily basis and add/update the chart accordingly.

Here is the data with ranges in Google sheet, python code I am trying with pygsheets and the chart I am getting using pygsheets.add_chart()

enter image description here

        # Inserting/Updating chart
        chart_title = "Title"
        # Range is dynamic, here is a sample range, check resp. sheet SS
        chart_range = [('F6', 'L6'), ('F7', 'L7'), ('F8', 'L8'), ('F9', 'L9')]
        chart_anchor_cell = 'F2'
        chart_domain = None
        
        # Check chart's existence
        found_charts = wks.get_charts(chart_title)
        if len(found_charts)!=0:
            print("Chart Found. Updating chart...")
            chart_obj = found_charts[0]
            # NOT SURE HOW TO UPDATE IT'S VALUES LIKE UPDATED RANGE, LEGEND, X&Y axis
            # .....................................................................
        else:
            print("Chart not Found. Creating chart...")
            chart_obj = wks.add_chart(domain=chart_domain, ranges=chart_range, title=chart_title,  chart_type=pygsheets.ChartType.LINE, anchor_cell=chart_anchor_cell)
            chart_json = chart_obj.get_json()
            # NOT SURE HOW TO UPDATE IT'S VALUES LIKE LEGEND, X&Y axis title, SIZE
            # .....................................................................

As the domain element is None, I am not able to use methods update_chart() and get_json(), so it seems impossible for me to update chart's attributes.

Can anybody, please tell me that how I can update chart's values with a Chart object?

Thanks in advance!

Ank Raw
  • 155
  • 10
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `update chart's values`. Can I ask you about the detail of your goal? – Tanaike Aug 28 '22 at 06:15
  • Hello @Tanaike, I want to update RANGE, LEGEND, X&Y axis title, SIZE etc. – Ank Raw Aug 28 '22 at 06:50
  • Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Aug 28 '22 at 11:39
  • Hello @Tanaike, I am creating a line chart from above shown google sheet data using python pygsheets library. After creating chart, I am not able to set it's values like: Chart RANGE, LEGEND, X&Y axis title, SIZE etc. So, I want to know how I can update these chart values with python pygsheets Chart class object(https://pygsheets.readthedocs.io/en/stable/chart.html). – Ank Raw Aug 28 '22 at 12:22
  • Thank you for replying. I apologize for my poor English skill, again. From your replying, unfortunately, I cannot still imagine your goal. By this, I cannot still think of a solution. But I would like to support you. And, I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill, again. – Tanaike Aug 29 '22 at 01:32
  • `domain` is only `None` by default. Doesn't mean you cannot set a (non-null) domain. Anyway, I'm not sure all of the properties you mentioned can be edited programmatically (check [this](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/charts) to check which properties can be edited). Also, would you be open to using the official Python library for Sheets API, instead of `pygsheets`? – Iamblichus Aug 29 '22 at 10:18
  • Hello @lamblichus, thanks for the reply. You are correct, domain is none by default. I have checked the GSAPI and yes the properties I want to be edited can be done from GSAPI, but I was having challenges in creating a LINE chart in GSAPI, so I didn't continued that way and working with pygsheets. Also, today I understood the concept of ranges and domains in wks.add_chart() method and created the same chart with non null domain and modified ranges and it works. I can get the json using chart_obj.get_json() but getting error with chart_obj.set_json(c_obj). – Ank Raw Aug 29 '22 at 13:15
  • Hi, can you clarify which error you are getting, and what code you are currently using? – Iamblichus Sep 01 '22 at 07:12

0 Answers0