在 Google Sheets 中获取汇率

先用 GOOGLEFINANCE 获取单元格里的当前或历史数据。需要 JSON、缓存或自定义函数时,再用 Apps Script。

exchangerate.dev更新于 2026 年 10 月 1 日阅读 6 分钟

如果数据只留在表格里,GOOGLEFINANCE 最简单。需要和应用使用同一套接口时,Apps Script 是后备方案。

要点
用 GOOGLEFINANCE 获取当前值和日期范围。
历史 GOOGLEFINANCE 数据无法从 Sheets API 或 Apps Script 读取。
FXRATE 自定义函数可调用 JSON 并使用缓存。
共享表格的 Apps Script 中不要放 API 密钥。
展示时保留 source 和观测时间。

先用 GOOGLEFINANCE

在单元格中使用 =GOOGLEFINANCE("CURRENCY:USDEUR");历史数据则加上开始日期、结束日期和 DAILY。报价可能延迟,历史值也不能通过 Sheets API 或 Apps Script 读取。

什么时候用 Apps Script

需要 JSON、自己的函数名或缓存时,可以使用 Apps Script。示例从匿名调用开始,并按货币对缓存十分钟。

粘贴 FXRATE 函数

把函数复制到扩展程序 → Apps Script。绑定脚本对表格编辑者可见,因此示例不会把密钥写入代码。

使用 =FXRATE()

保存脚本后,在单元格中输入 =FXRATE("USD","EUR")。更换两个 ISO 代码即可查询其他货币对。

刷新和安全

自定义函数不是实时流。如果共享表格需要稳定配额,请让表格调用服务器代理,并把密钥放在那里。

常见问题

Google Sheets
=GOOGLEFINANCE("CURRENCY:USDEUR")
=GOOGLEFINANCE("CURRENCY:USDEUR", "price", DATE(2026, 1, 1), DATE(2026, 1, 31), "DAILY")
Code.gs
// google-sheets: Apps Script Code.gs
const CACHE_TTL = 600; // Cache successful responses for 10 minutes.

/**
 * Returns an indicative exchange rate for one currency pair.
 * @param {string} from Three-letter base currency code.
 * @param {string} to Three-letter quote currency code.
 * @return {number|string} The rate or a readable error marker.
 * @customfunction
 */
function FXRATE(from, to) {
  from = (from || 'USD').toString().trim().toUpperCase();
  to = (to || 'EUR').toString().trim().toUpperCase();

  if (!/^[A-Z]{3}$/.test(from) || !/^[A-Z]{3}$/.test(to)) return '#NO_PAIR';

  const cacheKey = `fx_${from}_${to}`;
  const cache = CacheService.getScriptCache();
  const cached = cache.get(cacheKey);
  if (cached) return Number(cached);

  const url = `https://api.exchangerate.dev/v1/latest/${from}?symbols=${to}`;
  let resp;
  try {
    resp = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
  } catch {
    return '#FETCH_ERROR';
  }

  const code = resp.getResponseCode();
  if (code === 401 || code === 403) return '#ACCESS_ERROR';
  if (code === 429) return '#RATE_LIMIT';
  if (code !== 200) return '#ERROR';

  let data;
  try { data = JSON.parse(resp.getContentText()); }
  catch { return '#INVALID_RESPONSE'; }
  const rate = data && data.rates && data.rates[to];
  if (rate == null) return '#NO_PAIR';
  if (typeof rate !== 'number' || !Number.isFinite(rate) || rate <= 0) return '#INVALID_RESPONSE';
  cache.put(cacheKey, String(rate), CACHE_TTL);
  return Number(rate); // coerce so cells sum and sort
}
Sheets formula · pairs
// Euro per US dollar
=FXRATE("USD", "EUR")        // 0.9245

// Japanese yen per US dollar
=FXRATE("USD", "JPY")        // 157.32

// British pound per euro
=FXRATE("EUR", "GBP")        // 0.8531

// Indonesian rupiah per US dollar
=FXRATE("USD", "IDR")        // 16285.0
GET /v1/latest/USD?symbols=JPY
{
  "result": "success",
  "base": "USD",
  "source": "live",
  "market_session": "open",
  "timestamp": "2026-06-29T09:14:02Z",
  "data_updated_at": "2026-06-29T09:14:00Z",
  "rates": { "JPY": 157.32 },
  "sources": { "JPY": "live" },
  "notice": "Indicative rates, not for settlement."
}
Google Sheets · custom function
=FXRATE("USD", "EUR")
ER
exchangerate.dev
面向开发者的集成指南。

继续阅读