I'm trying to get text to auto-resize to fit within its cell with Google Sheets. I have cell widths set to a specific size and have things like emails and job titles that can get pretty long.
I know that Google Sheets doesn't natively support Shrink Text to Fit (WHY!!!!!!) like Excel does but is there a script I can run that will achieve the same thing?
Issue and workaround:
Unfortunately, in the current stage, there are no methods for automatically resize the font size for fitting in the cell width in the Spreadsheet service. So in this case, it is required to think of the workaround. But the direction for calculating the length of texts in the unit of pixel cannot be directly used. Because as a test case, when I compared the text length (pixel) calculated from the font size and the cell width (pixel), those were different. By this, in this answer, I would like to propose a workaround using other direction. The base flow of this workaround is as follows.
autoResizeColumn.By this flow, the text length to fit in the cell width can be automatically adjusted. When this flow is used, the sample script is as follows.
Sample script:
Please copy and paste the following script and set
targetRange. And run the script.toLargeistrue, when the text length is smaller than the cell width, the font size of the text becomes large. By this, the text length is matched to the cell width. WhentoLargeisfalse, when the text length is smaller than the cell width, the font size of the text not changed.Result:
In this demonstration, the lengths of texts in the cells "A1:A6" are matched to the cell width by changing the font size. In this case,
toLargeistrue.Note:
setFontSize(size)is "Integer". By this, the text length might not be exactly the same because the font size is required to be the integer type. So please be careful this.References: