Exchange Rates in Google Sheets: Pull Live Rates with Apps Script
6 min read
If you want exchange rates in Google Sheets that update on their own, the reliable approach is a small Apps Script that calls a rate API on a schedule and writes the results to a dedicated "Rates" tab. Your formulas then read from that tab like any other data.
The tempting alternative is a custom function that calls the API from every cell. It works for a demo, but it burns request units quickly and gives you no control over when values refresh. This guide shows the scheduled-tab pattern step by step, with code you can paste into the script editor.
Why one call per cell is the wrong default
A custom function such as =RATE("USD","EUR") looks elegant. In a real workbook it causes three problems:
- Cost grows with the sheet. A column of 500 converted amounts can mean 500 calls every time the sheet recalculates. On a metered API, that is 500 request units for data that barely changed.
- Rate limits. Plans have a per-second limit. Hundreds of cells evaluating at once can trigger
429responses, and your sheet fills with errors. - No audit trail. If every cell fetches its own rate at a slightly different moment, two cells in the same report can use different rates for the same pair. Finance teams hate that.
The scheduled-tab pattern fixes all three. You fetch each pair once per refresh, store it with its timestamp, and every formula in the workbook uses the same value until the next refresh.
Set up the Rates tab
Create a sheet named Rates with this header row and a few pairs:
| A: base | B: quote | C: rate | D: as_of | E: status |
|---|---|---|---|---|
| USD | EUR | |||
| USD | GBP | |||
| USD | JPY | |||
| EUR | CHF |
You only fill columns A and B. The script fills C, D and E. Columns D and E matter: the API returns an as_of timestamp and a status of fresh or stale with every rate, and you should keep both so a reader can see how old a number is.
Any pair works, even one no market quotes directly, because missing pairs are derived through USD, EUR or BTC. A derived rate reports the as_of of its oldest input. See the current-rate documentation for the exact response fields.
Store your API key safely
Do not paste the key into a cell or into the code. In the Apps Script editor, open Project Settings, scroll to Script Properties and add a property named GETRATES_API_KEY with your key as the value. Anyone who can edit the script can still read it, so share the spreadsheet with that in mind, and rotate the key in the dashboard if a collaborator leaves.
The refresh script
Open Extensions, then Apps Script, and paste this:
const BASE_URL = 'https://api.getratesapi.com/v1';
function apiGet_(path) {
const key = PropertiesService.getScriptProperties()
.getProperty('GETRATES_API_KEY');
const response = UrlFetchApp.fetch(BASE_URL + path, {
headers: { Authorization: 'ApiKey ' + key },
muteHttpExceptions: true,
});
const body = JSON.parse(response.getContentText());
if (response.getResponseCode() !== 200) {
// Match on the machine-readable code, not the message text.
throw new Error(body.code);
}
return body;
}
function refreshRates() {
const sheet = SpreadsheetApp.getActive().getSheetByName('Rates');
const lastRow = sheet.getLastRow();
if (lastRow < 2) return;
const pairs = sheet.getRange(2, 1, lastRow - 1, 2).getValues();
pairs.forEach(function (pair, i) {
const row = i + 2;
const base = String(pair[0]).trim().toUpperCase();
const quote = String(pair[1]).trim().toUpperCase();
if (!base || !quote) return;
try {
const r = apiGet_(
'/rates/' + encodeURIComponent(base) + '/' + encodeURIComponent(quote)
);
sheet.getRange(row, 3, 1, 3).setValues([
[Number(r.rate), new Date(r.as_of), r.status],
]);
} catch (e) {
// Keep the last good rate, show what went wrong.
sheet.getRange(row, 5).setValue('error: ' + e.message);
}
Utilities.sleep(600); // stay under a 2 requests/second limit
});
}
A few details are worth explaining:
muteHttpExceptions: truelets you read the JSON error body instead of getting a generic exception. Errors come back as{"code","message","request_id"}, and the code is what you should branch on. The error handling guide lists what each code means.- On failure the script leaves the previous rate in place and only changes the status cell. A stale-but-visible number with an error note is usually better than a blank.
Utilities.sleep(600)spaces the calls out so a plan with a low per-second limit does not returnrate_limit_exceeded. If you do hit a limit, the response includesretry_after_seconds. The rate limits documentation explains how limits are applied.
Run refreshRates once from the editor. Google will ask you to authorize the script the first time. Afterward, columns C to E should fill in.
Refresh on a schedule
Run this function once to install an hourly trigger:
function installTrigger() {
ScriptApp.newTrigger('refreshRates')
.timeBased()
.everyHours(1)
.create();
}
You can also create the trigger by hand under Triggers in the editor. Run installTrigger only once, or you will end up with duplicate triggers that each spend request units.
How often should you refresh? Match the cadence to the decision the sheet supports. Fiat rates and metals update every 60 seconds at the source and crypto every 10 seconds, but a monthly expense report does not need a rate from the last minute. Hourly is plenty for most dashboards. Once a day is fine for a reconciliation sheet.
Use the rates in formulas
Because the tab has a stable layout, a lookup is simple. In Google Sheets, FILTER returns the matching rate:
=B2 * FILTER(Rates!$C$2:$C, Rates!$A$2:$A="USD", Rates!$B$2:$B="EUR")
If B2 holds an amount in USD, that formula returns the amount in EUR. To avoid repeating the lookup, put the pair key in a helper column or wrap it in a named function.
Two habits keep the sheet trustworthy:
- Show the age. Add a cell next to your totals that displays the oldest
as_offrom the tab, for example=MIN(Rates!D2:D). Readers can then judge whether the totals are current. - Highlight stale rates. Add a conditional formatting rule on column E that colors cells equal to
stale. Stale rates are flagged rather than withheld, so the sheet keeps working, but you should know when it happens. Crypto becomes stale after 1 minute and fiat after 5 minutes without a fresh observation.
Know the precision limit of a spreadsheet
The API returns rates as exact decimal strings. Google Sheets stores numbers as double-precision floats, so the Number() call in the script is a deliberate trade-off. For reports, dashboards and planning, that is fine. Apply ROUND where you display or total amounts, using the right decimal places for each currency. Our rounding guide explains why.
For invoices, ledgers and anything that gets paid, do not make the spreadsheet the system of record. Convert in code with a decimal type, or call the convert endpoint, which returns an exact converted_amount, and store the rate and its as_of with the document.
Budget your request units
Each refresh costs one request unit per pair with the single-rate endpoint. The arithmetic is easy:
- 10 pairs refreshed once a day: 10 × 30 = 300 units per month.
- 10 pairs refreshed hourly: 10 × 24 × 30 = 7,200 units per month.
That second number is why the refresh interval matters more than the number of formulas in your workbook. Formulas are free because they read the tab. If your pairs share a base, the API can return up to 20 quotes for one base in a single call that still costs one unit, which cuts the bill sharply. We describe that in getting multiple exchange rates in one call, and the usage estimation guide walks through budgeting in more depth. Check the Apps Script editor's execution log and the usage endpoint now and then to confirm the real number.
Try it with the free plan
The free plan has no credit card requirement and 500 request units a month, enough for a handful of pairs refreshed daily. Sign up, copy a key into Script Properties, and test a pair in the dashboard playground before wiring it into the sheet. If your sheet grows, compare plans on the pricing page.