Google Sheets Script to Replace Formulas with Values
In this article
To replace formulas with their calculated values using a script, you use the Apps Script getValues() and setValues() methods. This process reads the current displayed results of your formulas into a temporary array and then writes those same values back over the original cells, effectively stripping the underlying logic while keeping the data intact. This is essential for freezing reports, preparing data for export, or preventing accidental changes to complex calculations in shared sheets.
What You'll Need
- A Google account with access to Google Sheets.
- A spreadsheet containing the formulas you wish to convert to static values.
- Basic familiarity with navigating the Google Sheets menu system.
- Browser access to the Google Apps Script editor.
Step-by-Step
- Open the Google Sheet containing the data you want to process.
- Navigate to the top menu and select Extensions > Apps Script. This will open a new browser tab with the script editor. Note that menu labels may differ slightly depending on your account type or region.
- In the script editor, you will see a default function called
myFunction(). Delete all existing code in the editor window to start with a clean slate. - Copy and paste the following code into the editor:
function replaceFormulasWithValues() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var range = sheet.getDataRange(); var values = range.getValues(); range.setValues(values); } - Click the disk icon (Save project) in the toolbar and give your project a name, such as "Formula Converter".
- Ensure the function
replaceFormulasWithValuesis selected in the dropdown menu next to the "Debug" button. - Click the Run button (the play icon).
- Because this script modifies your spreadsheet, Google will prompt you for permissions. Click Review Permissions.
- Select your Google account from the list.
- You may see a screen stating "Google hasn't verified this app" because you just wrote it. Click Advanced and then click Go to Formula Converter (unsafe) at the bottom.
- Click Allow to grant the script permission to access your spreadsheet.
- Return to your Google Sheet. You will notice that the cells which previously contained formulas now contain only the static results.
Common Mistakes
One of the most frequent errors is running the script on the wrong sheet. The code provided uses getActiveSheet(), which means it targets whatever tab happens to be open and active when you hit run. If you have a multi-tab workbook and run the script while looking at a summary page, you might accidentally overwrite formulas on that page instead of your raw data page. To avoid this, you can replace getActiveSheet() with getSheetByName("YourSheetName"), replacing "YourSheetName" with the actual name of the tab.
Another common mistake is forgetting that setValues() is a destructive action. Once the script runs and replaces a formula with a value, the original formula is gone. Unless you have a backup or a version history snapshot, you cannot "undo" the script execution with the standard Ctrl+Z / Cmd+Z keyboard shortcuts. Always create a duplicate of your tab before running a script that modifies data permanently.
Users also frequently encounter issues with the getDataRange() method. This method selects everything from cell A1 to the last cell that contains any content. If you have a large sheet with a few stray characters in a distant column (like column Z), the script will process the entire area, which can slow down the execution or overwrite formatting in areas you didn't intend to touch.
Tips
To make the script more precise, instead of using getDataRange(), you can define a specific range. For example, if you only want to convert formulas in column C, you can use sheet.getRange("C1:C100"). This prevents the script from touching other columns that may need to remain dynamic.
If you find yourself running this script daily, you can add a custom menu button to your spreadsheet so you don't have to open the Apps Script editor every time. Add this function to your script file:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Tools')
.addItem('Convert Formulas to Values', 'replaceFormulasWithValues')
.addToUi();
}
After saving this and refreshing your spreadsheet, a new menu called "Custom Tools" will appear next to "Help", allowing you to trigger the replacement with two clicks.
For very large datasets (thousands of rows), getValues() and setValues() are the most efficient methods because they perform "batch operations." Avoid using a loop that calls getValue() and setValue() on individual cells, as this will trigger a separate API call for every single cell and likely result in a "Script timeout" error.
Troubleshooting
If the script does not seem to do anything, first check the Execution Log at the bottom of the Apps Script editor. If you see a red error message, read the description. A common error is "Exception: The number of columns in the data does not match the number of columns in the range." This usually happens if you manually changed the range in the code but the array of values retrieved doesn't match the dimensions of the target range.
If you receive a "Permission Denied" error, it is likely because you are logged into multiple Google accounts in the same browser session. Google Apps Script often struggles with multi-login sessions. The simplest fix is to sign out of all accounts and sign back into only the one that owns the sheet, or open the sheet in an Incognito/Private browser window.
If the script runs but the values are incorrect (e.g., dates turning into long numbers), this is a formatting issue. The script moves the raw value, but the cell formatting might change. Select the affected columns in your sheet and go to Format > Number and select the correct format (e.g., Date or Currency) to restore the visual appearance.
Frequently Asked Questions
Can I use this script to replace only specific formulas?
The basic script replaces everything. To target specific formulas, you would need to use getFormulas() to check the content of each cell in a loop and only apply setValue() if the formula matches a specific pattern or keyword.
Will this remove conditional formatting?
No. This script only affects the value and the formula within the cell. Conditional formatting rules are stored as separate metadata for the range and will remain active even after the formula is replaced by a static value.
Does this work on mobile devices?
No. Google Apps Script cannot be written or triggered via the Google Sheets mobile app. You must set up the script and any custom menus on a desktop browser. However, if you have a time-based trigger set up, the script will run on the server regardless of whether the sheet is open on a mobile device.
Is there a limit to how many cells the script can process?
Yes. Google imposes a maximum execution time (usually 6 minutes for standard accounts). If your sheet has hundreds of thousands of cells, the script may time out. In those cases, it is better to process the data in smaller, defined chunks rather than using getDataRange().
Can I schedule this script to run automatically?
Yes. In the Apps Script editor, click the clock icon (Triggers) on the left sidebar. Click Add Trigger, select replaceFormulasWithValues, and choose a "Time-driven" event source to have it run hourly, daily, or weekly.