-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfredDATA.gs
54 lines (34 loc) · 1.39 KB
/
fredDATA.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Returns FRED®-Data (Federal Reserve Economic Data) by Series-ID
*
* @param {string} series_id
* Example: "GDP" (Gross Domestic Product)
*
* @param {boolean} parameter
* 0 (default) = Current Value | 1 = All Values
*
* @customfunction
* @return an array
*/
function fredDATA(series_id, parameter) {
var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //Change this variable to your API-Key
var http_response = UrlFetchApp.fetch('https://api.stlouisfed.org/fred/series/observations?' + 'series_id=' + series_id + '&api_key=' + api_key + '&file_type=json');
var api_data = JSON.parse(http_response.getContentText());
if (parameter) {
var observations_array = api_data.observations;
var results_array = [['Date', series_id]];
for (i = 0; i < observations_array.length; i++) {
if (Number(api_data.observations[i]['value'])) {
results_array.push([api_data.observations[i]['date'], Number(api_data.observations[i]['value'])]);
};
};
return results_array;
} else {
var observations_array = api_data.observations;
var results_array = [['Date', series_id]];
if (Number(api_data.observations[observations_array.length - 1]['value'])) {
results_array.push([api_data.observations[observations_array.length - 1]['date'], Number(api_data.observations[observations_array.length - 1]['value'])]);
};
return results_array;
};
}