Smart Systems for Home

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

Google Sheets Script to Refresh Formulas: Full Guide

· 6 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

To refresh formulas in Google Sheets using a script, you must write a function that clears the cells containing the formulas and then immediately re-inserts them. Because Google Sheets only recalculates a formula when its dependent cells change, functions like IMPORTXML, IMPORTHTML, or GOOGLEFINANCE often become "stale" and do not update in real-time. By programmatically removing and replacing the formula, you force the Google Sheets engine to treat the formula as new, triggering a fresh fetch of the data.

What You'll Need

  • A Google account with access to a Google Sheet.
  • The specific cell references or range (e.g., "A1:B10") that contain the formulas you wish to refresh.
  • Basic access to the Apps Script editor (built into Google Sheets).

Step-by-Step

  1. Open the Google Sheet that contains the formulas you want to refresh.
  2. In the top menu, navigate to Extensions > Apps Script. This will open a new browser tab with the Google Apps Script editor. Note that menu labels may differ slightly depending on your account type or region.
  3. 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.
  4. Copy and paste the following script into the editor. This script is designed to target a specific range, store the formulas, clear them, and put them back:
    function refreshFormulas() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var range = sheet.getRange("A1:A10"); // Change this to your actual range
      var formulas = range.getFormulas();
      
      range.clearContent();
      SpreadsheetApp.flush();
      range.setFormulas(formulas);
    }
  5. Replace "A1:A10" in the code with the actual range of cells that contain your formulas. If your formulas are scattered, you can create multiple range variables or use a loop.
  6. Click the Save icon (floppy disk) in the toolbar and give your project a name, such as "Formula Refresher".
  7. To test the script, ensure you are on the correct sheet tab in your spreadsheet, then go back to the script editor and click the Run button (the play icon).
  8. The first time you run the script, Google will prompt you for permissions. Click "Review Permissions", select your Google account, click "Advanced" (usually a small link on the left), and then click "Go to Formula Refresher (unsafe)". Finally, click "Allow". This is a standard security procedure for scripts you write yourself.
  9. Check your spreadsheet. You should see the formulas briefly disappear and then reappear, triggering a refresh of the data.
  10. To make this automatic, click the clock icon (Triggers) on the left-hand sidebar of the Apps Script editor.
  11. Click the "+ Add Trigger" button in the bottom right corner.
  12. Under "Choose which function to run", select refreshFormulas.
  13. Under "Select event source", choose "Time-driven".
  14. Under "Select type of time based trigger", choose your preferred interval (e.g., "Minutes timer" every 15 minutes, or "Hour timer" every hour).
  15. Click Save. Your formulas will now refresh automatically based on the schedule you set.

Common Mistakes

One of the most frequent errors is using getValue() instead of getFormulas(). If you use getValue(), the script captures the result of the formula (the number or text) rather than the formula itself. When the script writes that value back into the cell, it overwrites the formula with a static value, and the cell will never update again. Always ensure you are using the plural getFormulas() and setFormulas() when dealing with ranges.

Another common mistake is forgetting to use SpreadsheetApp.flush(). Google Apps Script batches changes to improve performance. If you clear the content and set the formulas in the same execution block without a flush, Google Sheets may optimize the operation and realize the end state is the same as the start state, effectively doing nothing. The flush() command forces the sheet to apply the "clear" action before moving to the "set" action.

Users also often apply the script to the entire sheet (e.g., sheet.getDataRange()) when they only have a few formulas. If your sheet contains thousands of rows of static data, clearing and resetting the entire range can lead to "Script timeout" errors or exceed the Google quota for API calls. Always target the smallest possible range that contains the formulas needing a refresh.

Tips

If you have formulas on multiple different tabs, you can modify the script to target specific sheets by name. Instead of getActiveSheet(), use getActiveSpreadsheet().getSheetByName("Sheet1"). This prevents the script from running on whatever tab happens to be open when the trigger fires, which is critical for automated time-driven triggers.

To avoid the "unsafe" warning for other collaborators, you can deploy the script as an add-on, though for most internal business use, the manual authorization process is sufficient. If you need to refresh a very large number of cells, consider using a "dummy" cell. You can write a formula that depends on a single cell (e.g., =IF(Z1=1, IMPORTXML(...), IMPORTXML(...))). Then, your script only needs to change the value of cell Z1 from 1 to 2 and back to 1. This forces every formula dependent on Z1 to recalculate without needing to rewrite every single formula in the sheet.

For those using IMPORTXML or IMPORTHTML, be mindful of the destination site's terms of service. Setting a trigger to refresh every minute may result in your IP address being temporarily blocked by the website you are scraping, as it looks like a bot attack. A 15-minute or 1-hour interval is generally safer and more respectful of server resources.

Troubleshooting

If the script runs without errors but the data does not change, the first step is to check if the data source itself has updated. If the external website or API hasn't changed its output, the formula will look the same even after a refresh. Try changing the URL in the formula slightly to see if it fetches new data.

If you encounter a "Permission Denied" error, it is usually because the script was shared with another user who hasn't authorized it. Each user who manually runs the script must go through the authorization flow. However, time-driven triggers run under the authority of the person who created the trigger, so they will continue to work even if other users cannot manually run the script.

If the script is running too slowly or hitting a timeout, reduce the size of the range being refreshed. Instead of refreshing a 100x100 block, try breaking the refresh into smaller chunks or using the "dummy cell" method mentioned in the Tips section. If the script fails to trigger entirely, check the "Executions" tab in the Apps Script editor to see the error logs. If you see "Limit Exceeded", you are likely running the trigger too frequently.

Frequently Asked Questions

Will this script delete my data?

The script uses clearContent(), which removes the formulas temporarily. Because it immediately follows this with setFormulas(), your formulas are restored instantly. However, it is always recommended to make a duplicate copy of your spreadsheet before running a new script for the first time.

Can I refresh formulas based on a button click?

Yes. You can go to Insert > Drawing, create a shape or button, and save it. Right-click the drawing in your sheet, click the three dots (menu), select "Assign script", and type refreshFormulas. Now, clicking the button will trigger the refresh.

Does this work with GOOGLEFINANCE functions?

Yes, it works with GOOGLEFINANCE, IMPORTXML, IMPORTHTML, and IMPORTDATA. These functions are the most common candidates for this script because they rely on external data that doesn't always trigger a recalculation on its own.

How often can I set the trigger to run?

The shortest time-driven trigger available in the standard menu is every minute. However, doing this for large sheets may lead to performance lag or hit Google's daily quota for URL fetch calls. Every 15 to 60 minutes is the recommended frequency for most users.

What happens if the sheet is closed?

Time-driven triggers run on Google's servers, not your local computer. This means the formulas will refresh in the background even if your browser is closed and your computer is turned off.