Wednesday, March 23, 2022

App Scripts are pretty neat

I was doing some analysis and looking to create a graph in my Google Spreadsheet. The data had gaps and I was looking for a quick/easy way to set all empty cells to zero. I found an example after searching that I turned into the following:

function fillBlanks() {
var sheet = SpreadsheetApp.getActiveSheet();
var sheetLR = sheet.getLastRow();
var sheetLC = sheet.getLastColumn();
var range = sheet.getRange(1, 1, sheetLR, sheetLC);
var values = range.getValues();
for (var r = 1; r < sheetLR; r++) {
for (var c = 0; c < sheetLC; c++) {
if(String(values[r][c]).trim() == "") {
values[r][c] = 0;
}
}
}
range.setValues(values);
}
fillBlanks()

Have your spreadsheet open, then run it from App Scripts. Feel free to take, modify and re-use as you see fit.