I am looking for a script that if any row's cell in say column C is not blank then I want that row's height to be set to 200 if it is blank then set height to "Fit to Data". Thanks in advance
1 Answer
To force a row height of 200 pixels, use Sheet.setRowHeightsForced()
. To get Fit to data, use sheet.setRowHeight()
and set the row height to the default of 21 pixels. Note that this method never makes the visible row height less than the default row height even when the data would require less.
Here's sample code that resizes rows as you edit column C
in row 63
or below in sheet 'Contract'
:
/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
if (!e) {
throw new Error(
'Please do not run the onEdit(e) function in the script editor window. '
+ 'It runs automatically when you hand edit the spreadsheet.'
);
}
autoChangeRowHeight_(e);
}
/**
* Changes row height depending on whether column C is blank.
*
* @param {Object} e The onEdit() event object.
*/
function autoChangeRowHeight_(e) {
let sheet;
if (e.range.columnStart !== 3
|| e.range.rowStart < 63
|| !(sheet = e.range.getSheet()).getName().match(/^(Contract)$/i)) {
return;
}
if (e.value) {
sheet.setRowHeightsForced(e.range.rowStart, 1, 200);
} else {
sheet.setRowHeight(e.range.rowStart, 21);
}
}
-
Thank you! I forgot to add that I only want this done on the sheet "Contract" and only from row 63 down. If you could modify the script that would be awesome...Thanks again! Commented Sep 11, 2022 at 0:21
-
Also, the data in "Contract" is Filtered data from another sheet. I made a simple spreadsheet and added your script and I worked perfect to the sheet with the data buy did not on the sheet that had the filtered data. Any advice? Commented Sep 11, 2022 at 0:48
-
Here is a link to the simple spreadsheet I made: docs.google.com/spreadsheets/d/… Commented Sep 11, 2022 at 1:14
-
Edited the code to add
if()
tests for the row number and sheet name. Thefilter()
requirement constitutes a different question. Please ask only one question per post. Consider using Data > Create a filter instead of thefilter()
function. Commented Sep 11, 2022 at 5:58