Google Apps Script: getRowHeight, setRowHeight
Copying and pasting data while maintaining row heights and widths can be a frustrating business in Google Apps Script. You see, there is a difference in how to set column width and set row heights.
Let’s say we have already copied our data over with something like:
sourceDataRange.copyTo(destination)
Google Apps Script provides us with a great set of enumerators for copying and pasting, the CopyPasteType
.
To ensure that we maintain the same column width in our destination that was copied from our source, we need to run another copyTo()
method of our sourceDataRange
with the CopyPasteType.PASTE_COLUMN_WIDTHS
. So all together it might look a little like this:
1 2 3 |
sourceDataRange.copyTo(destination); sourceDataRange.copyTo(destinationStart, SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTH); |
Okay, okay, a little extra work, but pretty straight forward. We get our column width in the end.
Now by this stage, you might be thinking, Yagi, why are you even blogging about this. Seems pretty straight forward, ya just gotta do the same with heights!
Nope. This is the point where we just have to remind ourselves that we love Google Apps Script for her flaws and all. Right? Right?!
For, I am sure, a very logical reason for the developers of Google Apps Script, copying and pasting the width has to be done row-by-row.
Here’s how you would do it the long way.
Scroll down to The Shortcut if you just want to copy and paste the functions into your code.