cpi-feed/Code.gs
2023-01-21 20:42:45 -05:00

43 lines
1.2 KiB
JavaScript

// vim:filetype=javascript
const defaultSeriesURL = "https://api.bls.gov/publicAPI/v2/timeseries/data/";
const defaultSeriesID = "CUUR0200SA0";
const defaultStartYear = "2014";
function CURRENTCPI(blsToken, options) {
var seriesURL = options?.seriesURL || defaultSeriesURL;
var seriesID = options?.seriesID || defaultSeriesID;
var startYear = options?.startYear || defaultStartYear;
var endYear = options?.endYear || new Date().getFullYear().toString();
var reqOptions = {
"method": "post",
"payload": JSON.stringify({
"seriesid": [seriesID],
"startyear": startYear,
"endyear": endYear
}),
"contentType": "application/json",
"headers": {
"Authorization": "token "+blsToken,
"Content-Type": "application/json"
}
};
var resp = UrlFetchApp.fetch(seriesURL, reqOptions);
var rawData = resp.getContentText().toString().trim();
var parsedData = JSON.parse(rawData);
var respData = parsedData.Results.series[0].data;
var rows = [["year", "period", "period_name", "value"]];
for (var i = 0; i < respData.length; i++) {
rows.push([
respData[i].year,
respData[i].period,
respData[i].periodName,
respData[i].value
]);
}
return rows;
}