Google Sheets Script to Replace Blanks: How-To
In this article
You can replace blank cells in Google Sheets with a value using a short Apps Script that loops through your range and writes a replacement wherever a cell is empty. The fastest working method is to open Extensions > Apps Script, paste a small function that uses getRange(), getValues() and setValues(), then run it once from the editor. This is useful for filling gaps before sorting, importing into another system, or making formulas behave consistently.
What You'll Need
- A Google account with edit access to the spreadsheet.
- The spreadsheet open in a desktop browser (the Apps Script editor is not available in the mobile Sheets app).
- A clear idea of which range to target and what value should replace the blanks (for example 0, "N/A", or a date).
- Basic comfort with copying and pasting code; no prior programming needed.
Step-by-Step
- Make a copy first. In Google Sheets, choose File > Make a copy and work on the copy. Scripts that write values cannot be undone with Ctrl+Z after the function finishes, so a backup is your safety net.
- Open the script editor. With the spreadsheet open, go to Extensions > Apps Script. A new tab opens with a code editor. Menu labels can differ slightly by account type or region, but Extensions > Apps Script is the standard path on desktop.
- Clear the placeholder code. Delete the default
function myFunction() {}block so you start with an empty file. - Paste a replacement function. Use this script, which fills blanks in a named range with a value you set:
Changefunction replaceBlanks() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var range = sheet.getRange('A1:D100'); var values = range.getValues(); var replacement = 'N/A'; for (var r = 0; r < values.length; r++) { for (var c = 0; c < values[r].length; c++) { if (values[r][c] === '') { values[r][c] = replacement; } } } range.setValues(values); }'Sheet1'to your tab name,'A1:D100'to your actual range, and'N/A'to whatever should fill the blanks. - Save the project. Click the save icon (or press Ctrl+S / Cmd+S). Give the project a name when prompted.
- Authorize the script. Click Run. Google will ask for permission the first time. Choose your account, click Advanced, then proceed to the script (the exact wording varies), and allow access. This is normal for any script that edits your file.
- Check the result. Return to the spreadsheet tab. The blank cells in your range should now contain the replacement value. If nothing changed, confirm the tab name and range in the code match your sheet exactly.
- Optional: add a menu button. To avoid opening the editor each time, paste a second function that creates a custom menu:
Save, reload the spreadsheet, and the menu appears. Note thatfunction onOpen() { SpreadsheetApp.getUi() .createMenu('Tools') .addItem('Replace blanks', 'replaceBlanks') .addToUi(); }onOpenruns automatically when the file opens, so the menu shows up on the next load.
Common Mistakes
- Wrong sheet name.
getSheetByName('Sheet1')fails silently or errors if your tab is called something else. Copy the tab name exactly, including spaces. - Range too small or too large. A range like A1:D100 only covers those cells. If your data extends to row 500, blanks below row 100 stay blank.
- Confusing empty strings with formulas returning "". A formula that outputs an empty string is not the same as a truly blank cell. The script above checks for
'', which catches both empty cells and empty-string results, but if you need to preserve formulas, use a different approach that checksisBlank()on the cell instead. - Running the script on the wrong tab. The active spreadsheet may have multiple tabs; the code targets the one named in
getSheetByName, not the one you are viewing. - Skipping authorization. If you close the permission dialog, the script will not run. Re-run and complete the flow.
- Forgetting to save. Unsaved code runs the old version. Save before running.
Tips
- Test on a small range first, such as A1:B5, before running on a large dataset.
- Use a replacement value that matches the column's data type. Putting text like "N/A" in a numeric column will break SUM and AVERAGE formulas; use 0 instead.
- If you only need to fill blanks visually, consider a formula approach in a helper column, such as
=IF(A1="","N/A",A1), which leaves the original data untouched. - For very large ranges, the loop approach is fine but slower; limit the range to the used area.
- Keep a copy of the original data in a separate tab so you can compare before and after.
Troubleshooting
If the script does nothing, first check that you saved the project and that the tab name in the code matches the tab exactly. Next, confirm the range covers the cells you expect. If you see an authorization error, re-run and complete the permission steps; if the error mentions a missing method, check that you pasted the code into the correct project bound to that spreadsheet.
If the script runs but fills the wrong cells, your range is likely offset. Remember that getRange('A1:D100') starts at A1; if your data starts at row 5, adjust the range accordingly. If you get a "Cannot read property" error, the sheet name is probably wrong or the tab was renamed.
If the menu does not appear after adding onOpen, reload the spreadsheet. Custom menus only appear after the file reloads, and they will not appear in the mobile app.
Frequently Asked Questions
Can I replace blanks with a formula instead of a script?
Yes. In a helper column, use =IF(ISBLANK(A1),"N/A",A1) and fill down. This avoids editing the original cells but requires an extra column.
Will the script overwrite my formulas?
The example script reads values and writes values, so any formulas in the range are replaced by their results. If you need to keep formulas, target only truly blank cells and skip cells containing formulas.
Does this work on the Google Sheets mobile app?
No. Apps Script editing and running require a desktop browser. You can view the results on mobile after running the script on desktop.
Can I undo the script's changes?
Not with Ctrl+Z after the function completes. That is why making a copy first is recommended. You can manually restore from the copy.
How do I replace blanks only in one column?
Change the range to a single column, such as 'A1:A100', and keep the rest of the code the same.
Is there a built-in Find and Replace for blanks?
Google Sheets' Find and Replace does not reliably target empty cells. The script or helper-column formula are the practical options.