0
votes

I am currently trying to write some Google Apps Script script that would allow my Google sheet table to fit to data, but with some row height cap.

The easiest way that came into my mind was first fit everything to data, and then walk over every row to check whether its height is more than some value and then fix it if it is too high.

  sheet.autoResizeRows(startRow, numRows)

  for (var i=0;i<numRows;i++){
    var curHeight = sheet.getRowHeight(startRow + i)
    Logger.log(curHeight)
    if (curHeight > 50){
      sheet.setRowHeight(startRow + i, 50)
    }
  }

However, what I found out is that .getRowHeight() always returns 20.0 - the default value.

Thus, do I miss something or .getRowHeight() does not work properly with fit to data rows, and if not is there a workaround?

2
can you share a bit more code please? i can not see how "sheet" is defined..i am also wondering your "for loop" it looks like you looping over the same row...of course it can only return the same value :oCrissCrossCrass

2 Answers

0
votes

your code is perfect. So by-default the row size of google spreadsheet is 20 or 21 pixel. hence:

  • if you want to set a fixed provided height to all rows then use "if(curHeight != 50)" which will set height irrespective of whether the current height is higher or lower than the new value provided.
  • Else, if you just want to check your script, change the value to a value lesser than the default height i.e. if(curHeight > 10)
0
votes

can I ask if you ever got a solution to this?

I'm having exactly the same problem. I wrote a function to "prettify" a sheet to make all the row heights the same (based on the greatest row height required to fit the data), but it fails because, as you found, sheet.getrowHeight() always seems to return the default value of 20 pixels, rather than the actual row height on the sheet.