/** * Agent Data Hub in Google Sheets (adh-sheets-script/1): official figures with source and licence, no key. * Paste into Extensions > Apps Script, save, then type into any cell (a sheet set to German uses ; between arguments): * =ADH_LATEST("fx.EUR.USD") newest value * =ADH_SERIES("fx.EUR.USD", "2025-01-01", "2025-12-31") date and value, oldest first (from and to are optional) * =ADH_FIND("inflation germany") the first three series with id, unit and source * Help: https://api.agentdatahub.de/sheets/script ยท src=script only counts how often this script is used; nothing about you is kept. */ var ADH_BASE = 'https://api.agentdatahub.de'; function adhGet_(path) { var url = ADH_BASE + path + (path.indexOf('?') < 0 ? '?' : '&') + 'src=script'; var cache = CacheService.getScriptCache(), hit = cache.get(url); if (hit !== null) return hit; var res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }), body = res.getContentText(); if (res.getResponseCode() !== 200) throw new Error('Agent Data Hub ' + res.getResponseCode() + ': ' + body.slice(0, 300)); if (body.length < 90000) cache.put(url, body, 3600); return body; } function adhText_(v, tz) { if (v && typeof v.getTime === 'function') return Utilities.formatDate(v, tz, 'yyyy-MM-dd'); return String(v == null ? '' : v).trim(); } function adhCell_(s, isDate, tz) { if (isDate && /^\d{4}-\d{2}(-\d{2})?$/.test(s)) return Utilities.parseDate(s.length === 7 ? s + '-01' : s, tz, 'yyyy-MM-dd'); var n = Number(s); return s !== '' && isFinite(n) ? n : s; } /** Newest value of a series. Optional refresh: a cell; when its value changes, the value is loaded again. @customfunction */ function ADH_LATEST(id, refresh) { return adhCell_(adhGet_('/api/v1/series/' + encodeURIComponent(adhText_(id)) + '?format=value').trim(), false); } /** Date and value of a series, oldest first; from and to as a date, a year or YYYY-MM-DD (optional). @customfunction */ function ADH_SERIES(id, from, to) { var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), f = adhText_(from, tz), t = adhText_(to, tz); var q = '?format=csv' + (f ? '&from=' + encodeURIComponent(f) : '') + (t ? '&to=' + encodeURIComponent(t) : ''); var rows = Utilities.parseCsv(adhGet_('/api/v1/series/' + encodeURIComponent(adhText_(id)) + q)); return rows.filter(function (r) { return r.join('') !== ''; }).map(function (r, i) { return i === 0 ? r : r.map(function (s, j) { return adhCell_(s, j === 0, tz); }); }); } /** The first three series for a few words: id (for ADH_LATEST), title, unit, source, newest date. @customfunction */ function ADH_FIND(words) { var r = JSON.parse(adhGet_('/api/v1/series?q=' + encodeURIComponent(adhText_(words)) + '&limit=3')); if (!r.series || !r.series.length) return 'No series found. Try English words, e.g. "inflation germany".'; return [['id', 'title', 'unit', 'source', 'newest']].concat(r.series.slice(0, 3).map(function (s) { return [s.id, s.title, s.unit || '', s.source || '', s.to || '']; })); }