Google Sheets Script to Reformat Cells: Step-by-Step Guide
In this article
You can reformat cells in Google Sheets with a script by opening the script editor, writing a function that uses setNumberFormat, setFontWeight, setBackground and similar methods, then running it or triggering it. The shortest working set: open Extensions > Apps Script, paste a function that targets a range, save, and click Run. This guide walks through that process, plus how to make the formatting repeatable and safe.
What You'll Need
- A Google account with edit access to the spreadsheet you want to reformat.
- A Google Sheets file open in a desktop browser (the script editor is not available in the mobile Sheets app).
- Basic familiarity with cell references (for example,
B2:D50) so you can point the script at the right range.
Step-by-Step
- Open the script editor. In Google Sheets, go to Extensions > Apps Script. A new tab opens with a code editor bound to that spreadsheet. Menu labels can differ slightly by version, device or region, but this path is current for the desktop web app.
- Clear the placeholder code. Delete the default
function myFunction() {}block so you start with an empty file. - Write a formatting function. Paste a function that gets the active sheet, selects a range, and applies formatting. For example:
Replacefunction reformatCells() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var range = sheet.getRange('A2:D100'); range.setNumberFormat('0.00'); range.setFontWeight('bold'); range.setBackground('#f3f3f3'); range.setHorizontalAlignment('center'); }'Sheet1'with your tab name and'A2:D100'with your range. The methods used here are standard Apps Script range methods and work on any sheet. - Save the project. Click the save icon (or press Ctrl+S / Cmd+S). Give the project a name when prompted so you can find it later.
- Authorize the script. Click Run. The first time, Google asks for permission. Choose your account, click Advanced if prompted, then Go to project (unsafe) and Allow. This is normal for a script you wrote yourself; only grant access to scripts you trust.
- Check the result. Switch back to the Sheets tab. The target range should now show the number format, bold text, background color and alignment you set. If nothing changed, confirm the tab name and range in the script match the sheet exactly.
- Make it repeatable (optional). To run the formatting on a schedule or when the file opens, use a trigger: in the Apps Script editor, click the clock icon (Triggers), then Add Trigger, choose your function, select Time-driven or From spreadsheet > On open, and save. This is useful for monthly reports or templates that get reused.
- Test on a copy first. Before running on live data, duplicate the spreadsheet (File > Make a copy) and run the script there. Formatting changes are easy to undo with Ctrl+Z / Cmd+Z immediately after a run, but a copy avoids surprises.
Common Mistakes
- Wrong sheet name.
getSheetByName('Sheet1')fails silently or throws an error if the tab is named differently. Check the tab label exactly, including spaces. - Range includes headers. Applying a number format to a header row turns text into numbers or shows errors. Start ranges below the header, for example
A2:D100. - Forgetting to save before running. Apps Script runs the saved version, not unsaved edits. Save first, then run.
- Assuming formatting carries to new rows. A script formats the range you specify at the moment it runs. New rows added later are not automatically formatted unless you use a trigger or an
onEditfunction. - Using the mobile app. The Apps Script editor is not available in the Sheets mobile app. Use a desktop browser for setup.
Tips
- Use
getDataRange()instead of a fixed range when you want the script to cover all data on the sheet, including rows added later:sheet.getDataRange().setNumberFormat('0.00'). - Store format values in variables at the top of the function so you can change them in one place.
- Combine formatting calls on the same range object to reduce the number of operations and keep the script fast.
- Name your functions clearly (for example,
formatCurrencyColumn) so triggers and the Run menu are easy to navigate. - Keep a plain-text copy of your script in a separate document. If the spreadsheet is deleted, the bound script goes with it.
Troubleshooting
- “Script function not found” error. The function name in the Run dropdown does not match the code. Select the correct function name from the dropdown next to Run.
- Authorization loop. Sign out of all Google accounts, sign back in with the account that owns the file, and run again. If the file is shared, make sure you have edit access.
- Formatting applies to the wrong tab. The script uses
getSheetByName, which targets one tab. Confirm the name, or switch togetActiveSheet()if you want the currently visible tab. - Numbers show as text. If cells were stored as text,
setNumberFormatwill not convert them. Userange.setValue()or manually convert the column to numbers first. - Nothing happens on open. Check that the trigger is set to From spreadsheet > On open and that the function is saved. Triggers do not run on the mobile app.
- Unexpected changes to other cells. Undo immediately with Ctrl+Z / Cmd+Z, then review the range in your script. Run on a copy until the range is correct.
Frequently Asked Questions
Can I reformat cells without writing code?
Yes. Select the cells and use the toolbar or Format menu. A script is only needed when you want to repeat the same formatting automatically or across many sheets.
Does the script work on the Google Sheets mobile app?
No. The Apps Script editor is desktop-only. You can run a script from a computer, and the formatting will appear on mobile, but you cannot edit or trigger scripts from the mobile app.
Will the script overwrite my existing formatting?
Only the properties you set are changed. If you call setBackground, only the background color changes; other formatting stays as it was. Calls like clearFormat() remove formatting and should be used with care.
Can I undo a script's changes?
Yes, immediately after a run you can press Ctrl+Z / Cmd+Z in the spreadsheet. Once you close the file or make further edits, undo history may not cover the script changes.
How do I format new rows automatically?
Use an onEdit or time-driven trigger that reapplies the formatting to the full data range. A one-time run only affects the rows that exist at that moment.
Is it safe to authorize a script I wrote?
Yes, for scripts you write or review yourself. Only grant permissions to code you understand, and avoid running scripts from unknown sources.