Skip to content

Instantly share code, notes, and snippets.

@aflansburg
Created December 7, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflansburg/d307d072ca5b990e3280e5bb197b651f to your computer and use it in GitHub Desktop.
Save aflansburg/d307d072ca5b990e3280e5bb197b651f to your computer and use it in GitHub Desktop.
Remove empty rows from Google Sheets (Gsheets)
function onInstall(e){
onOpen(e);
}
function onOpen(e){
var menu = SpreadsheetApp.getUi().createAddonMenu();
menu.addItem('Remove Empty Rows', 'removeEmptyRows');
menu.addToUi();
}
function removeEmptyRows() {
showAlert('start');
SpreadsheetApp.getActive()
.getSheets()
.forEach(function (s) {
c = 0;
s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns())
.getValues()
.forEach(function (r, j) {
if (r.toString()
.replace(/,/g, "")
.length == 0) {
s.deleteRow((j += 1) - c)
c += 1;
}
})
})
showAlert('end');
}
function showAlert(locator){
var ui = SpreadsheetApp.getUi();
if (locator == 'start'){
ui.alert('Empty rows will be deleted. Please leave the sheet open until the process completes.\nPress ok to continue.!');
}
if (locator == 'end'){
ui.alert('Empty rows removed.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment