Last active
May 13, 2023 05:31
-
-
Save btamayo/3374b32a3b10ae55ee15f00f39c91b0b to your computer and use it in GitHub Desktop.
How to programmatically pad a cell's height in Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ResizeRow() { | |
// Be careful about this. If you change this to `getActiveSpreadSheet()`, it will be super confusing: | |
// see https://www.reddit.com/r/GoogleAppsScript/comments/10dbe41/comment/j4knea5 | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var selection = sheet.getSelection(); | |
var activeRange = selection.getActiveRange(); | |
var rowsInSelection = activeRange.getNumRows(); | |
var cell = selection.getCurrentCell(); | |
var row = cell.getRow(); | |
var col = cell.getColumn(); | |
// Leaving these as an example of how to log and for debugging | |
Logger.log('[Selection] Row: ' + row); | |
Logger.log('[Selection] Col: ' + col); | |
Logger.log('[Selection] rowsInSelection: ' + rowsInSelection); | |
for (let i = 0; i < rowsInSelection; i++) { | |
row = row + i; | |
Logger.log('[Loop] Row: ' + row); | |
Logger.log('[Loop] Col: ' + col); | |
range = sheet.getRange(row, col) | |
cell = sheet.setCurrentCell(range) | |
Logger.log('[Range] cell: ' + cell.getA1Notation()); | |
height = cell.getHeight() | |
sheet.setRowHeight(row, height + 100); | |
cell.setVerticalAlignment("middle"); | |
cell.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP); | |
} | |
return | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You technically don't need the for loop (I think macros act on a cell at a time for an entire selection), but just did it this way because I adapted it from an app script