Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Script to Replace Values: How-To

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can replace values across a whole Google Sheet with a short Apps Script that loops through the cells and swaps text or numbers, which is faster and more repeatable than Find and Replace when you need to run the same change regularly. The quickest working version is to open Extensions > Apps Script, paste a small function that reads your range with getValues(), maps each cell through a replace rule, and writes it back with setValues(). Run it once from the editor, authorise it, and the sheet updates in place.

What You'll Need

  • A Google account with edit access to the spreadsheet you want to change.
  • The spreadsheet open in a desktop browser. The Apps Script editor is not available in the Google Sheets mobile app, so do this on a computer.
  • A clear idea of the exact values to find and what to replace them with, including whether the match should be case-sensitive.
  • A backup copy of the sheet (File > Make a copy) if the data matters, because a script that writes back cannot be undone with Ctrl+Z once it has run.

Step-by-Step

  1. Open your spreadsheet and confirm which sheet tab and which column or range holds the values. Note the tab name exactly as it appears at the bottom.
  2. Go to Extensions > Apps Script. A new tab opens with a code editor and a default myFunction stub. Menu labels can differ slightly by account type and region, but Extensions > Apps Script is the standard path on desktop.
  3. Delete the stub and paste a function like this, adjusting the sheet name, range and replacement pairs:
    function replaceValues() {
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
      const range = sheet.getRange('A2:A1000');
      const values = range.getValues();
      const rules = [
        ['Old Text', 'New Text'],
        ['N/A', '']
      ];
      const updated = values.map(row => row.map(cell => {
        let out = cell;
        rules.forEach(([find, replace]) => {
          if (typeof out === 'string') {
            out = out.split(find).join(replace);
          }
        });
        return out;
      }));
      range.setValues(updated);
    }
  4. Click the save icon (or press Ctrl+S / Cmd+S) and give the project a name when prompted.
  5. Click Run. The first time, Google shows an authorisation screen. Choose your account, click Advanced if the app is marked unverified, then allow the script to view and edit your spreadsheets. This is the standard consent flow for your own bound script.
  6. Switch back to the spreadsheet tab. The target range should now show the replaced values. If nothing changed, check that the sheet name in the code matches the tab name exactly, including spaces and capitalisation.
  7. To run it again later without opening the editor, add a custom menu. Paste a second function such as function onOpen() { SpreadsheetApp.getUi().createMenu('Tools').addItem('Replace values', 'replaceValues').addToUi(); }, save, then reload the spreadsheet. A Tools menu appears with your item.

Common Mistakes

  • Using replace() on numbers. The JavaScript string method only works on strings. Convert with String(cell) first if the column mixes text and numbers, or handle numbers separately.
  • Forgetting that getValues() returns a 2D array. Every row is an array, so a single loop over the outer array gives you rows, not cells. Use a nested map or loop as shown.
  • Mismatched range size. setValues() throws an error if the output array is not the same shape as the range. Do not filter rows out before writing back.
  • Case sensitivity. split().join() is case-sensitive, so "n/a" will not match "N/A". Add both variants to the rules array or convert to lower case for the comparison.
  • Running on the wrong tab. getActiveSheet() depends on which tab is active; getSheetByName() is safer for scheduled or menu-driven runs.
  • No backup. A script overwrite is permanent. Make a copy first if the data is not reproducible.

Tips

  • Test on a small range first, such as A2:A10, before widening it.
  • Use range.getDisplayValues() instead of getValues() if you want to match what the cell shows (formatted dates, currency) rather than the underlying value.
  • For whole-sheet replacement, use sheet.getDataRange() so the range grows with your data.
  • Keep the rules array at the top of the function so it is easy to edit later.
  • If you need the change to run on a schedule, use a time-driven trigger from the Triggers page in the Apps Script editor rather than relying on manual runs.

Troubleshooting

If the script runs without an error but nothing changes, the most likely cause is a range or sheet name that does not match your data. Log the values with Logger.log(values) and check the execution log. If you see an authorisation error, run the function again and complete the consent screen; if your organisation blocks unverified apps, ask your Workspace admin. If setValues reports a dimension mismatch, confirm the output array has the same number of rows and columns as the input. If the menu item does not appear after adding onOpen, reload the spreadsheet tab; simple triggers do not fire until the document reopens. If a replacement produces unexpected partial matches, remember that split().join() replaces every occurrence inside a cell, so "cat" will also change "category". Use exact-match logic with === if you only want whole-cell swaps. Finally, if you have overwritten data by mistake, open File > Version history and restore an earlier version; that is the reliable undo for script edits.

Frequently Asked Questions

Can I replace values without writing code?

Yes. Edit > Find and replace works for one-off changes and supports match case and regular expressions. A script is better when you repeat the same replacement often or need it to run on a schedule.

Does the script work on the Google Sheets mobile app?

No. You can run an existing script from the mobile app only if it is attached to a button or menu, and the Apps Script editor itself is desktop-only. Author and edit scripts in a browser.

Can I undo a script that already ran?

Not with Ctrl+Z. Use File > Version history to restore a previous version of the spreadsheet, which is why making a copy first is recommended.

Will the script replace values in formulas?

getValues() returns the calculated result of a formula, not the formula text. Writing values back will overwrite the formula with a static value. If you need to keep formulas, restrict the range to non-formula columns.

Can I replace values across multiple sheets at once?

Yes, loop over SpreadsheetApp.getActiveSpreadsheet().getSheets() and apply the same logic to each sheet's data range. Skip sheets you do not want touched by name.

Is there a limit on how much data the script can process?

Apps Script has execution time limits that vary by account type, and very large ranges are slow because every cell is read and written. For big sheets, process in chunks or use a batch approach rather than one giant range.