0

I defined a python class to work with Google Sheets API v4. And I created this method to auto resize a column according to its content, following the instructions in here:

    def autofit_col(self, sheet_id, start_col=0, end_col=1000):
        """
        Resize column to fit to data.
        
        :param sheet_id: sheet id
        :param start_col: starting column, first column is 0
        :param end_col: final column plus 1
        """
        file_id = self.file_id 
        
        request = [{"autoResizeDimensions": {
                        "dimensions": {
                            "sheetId": sheet_id,
                            "dimension": "COLUMNS",
                            "startIndex": start_col,
                            "endIndex": end_col
                        }
                  }}]
        
        self.batch_update(file_id, request)

But it is not working, and I can't find the error... The same is happening in the case of ROWS.

It doesn't crash or show any warning or error, it simply does nothing.

The class is well defined because other methods are working properly.

I hope someone can find the problem and help me to fix it.

Many thanks!

4
  • Could you share the spreadsheet and the code for execute_requests() so I can verify the structure of your request?
    – Nikko J.
    Dec 23, 2020 at 21:53
  • From The class is well defined because other methods are working properly., for example, how about confirming the Spreadsheet ID and sheet ID, again? Because when your request body is used as {request: request} for the batchUpdate method, the request body works. I was worry that the Spreadsheet ID and sheet ID might be different from your target.
    – Tanaike
    Dec 24, 2020 at 1:54
  • self.execute_requests(file_id, request) is simply the same than self.batch_update(file_id, request) (I'm sorry I just chaged the name) Dec 26, 2020 at 4:56
  • the Spreadsheet ID and sheet ID are correct because other requests on them are updating properly Dec 26, 2020 at 4:59

1 Answer 1

2

It is actually working, I was just executing it in the wrong order...

I was also expecting it to resize empty columns to 100, and it is not doing so (and this confused me). It does nothing to empty columns (I had to create another method for these cases).

And it doesn't work exactly the same as the "manual" method. For example, if I manually "fit to data" a certain column, it resizes to 55, but if I do it through this method, it resizes to 60.

Thank you for your comments, they helped me to realize my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.