Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
311 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...