r/GoogleAppsScript • u/Left-Bathroom-9154 • Dec 31 '25
Resolved Array Find and Replace - Help!
Hello, I am an engineer trying to do some programming to help with a spreadsheet I am working on. I'll preface by saying I don't know how to code, especially with arrays, but I usually know enough to Google it and figure it out. I have a solution that is kind of working, but I know there is a better way to do this. I am also having trouble with inserting a formula. More info below...
I am trying to create a function that will check each cell in a range to see if it is blank. If it is blank, it needs to be replaced with either a zero or a formula, depending on what column it is in. My current code is below. Based on my research, I think using the map function would be better, but I couldn't figure it out.
function BlankCellReset() {
var sheet = SpreadsheetApp.getActiveSheet();
var bigRange = sheet.getRange("Cranes_Data");
var bigNumRows = bigRange.getNumRows(); //used to find out how many rows are in the table, since it varies.
var smallRange = bigRange.offset(0,13,bigNumRows,8) //filters down to just the cells I want to check
var smallValues = smallRange.getValues();
console.log(smallRange.getValues()); //just used for testing
for (var i = 0; i < smallValues.length; i++) { // Iterate through rows
for (let j = 0; j < smallValues[i].length; j++) { // Iterate through columns
switch (smallValues[i][j]) {
case smallValues[i][1]: //column zero to one
if (smallValues[i][j] === '') { //checks if blank
smallValues[i][j] = "0"; //value to take its place
break;
}
case smallValues[i][2]:
if (smallValues[i][j] === '') {
var copyFormula = bigRange.offset(bigNumRows,16).getDataSourceFormula.;
smallValues[i][j] = copyFormula;
break;
}
case smallValues[i][3]:
if (smallValues[i][j] === '') {
smallValues[i][j] = "0";
break;
}
case smallValues[i][4]:
if (smallValues[i][j] === '') {
smallValues[i][j] = "=N4*O4"; //was using a placeholder but needs to be the correct range not a string.
break;
}
case smallValues[i][6]:
if (smallValues[i][j] === '') {
smallValues[i][j] = "0";
break;
}
case smallValues[i][7]:
if (smallValues[i][j] === '') {
smallValues[i][j] = "=N7*O7";
break;
}
}
}
}
console.log(smallValues); //used for testing
smallRange.setValues(smallValues)
}

If I could have some help making this code work a bit better, and help with figuring out how to insert the formulas in and have them use the intended ranges I would greatly appreciate it.
View all comments
Show parent comment
2
u/zmandel Dec 31 '25
haha that was my point. once you get it into your flow it accelerates a lot your learning. Good that you are digging into why the code works, that the way.
look into using clasp, that reduces the friction as you use genAI from vscode instead of the browser editor.