-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetValueByDateAndKey.gs
More file actions
29 lines (28 loc) · 1001 Bytes
/
Copy pathGetValueByDateAndKey.gs
File metadata and controls
29 lines (28 loc) · 1001 Bytes
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
function valueByDateAndKey(fecha, key) {
var today = new Date(fecha);
//If is a weekend day use previous friday
if(today.getDay() == 6 || today.getDay() == 0) {
if(today.getDay() == 6) {
today.setDate(today.getDate() - 1);
} else {
today.setDate(today.getDate() - 2);
}
};
var data = fetchValue(key, today);
//Iterates previous days in case that the value for the specified date is not valid
while (data.data[0] == undefined) {
today.setDate(today.getDate() -1);
data = fetchValue(key, today);
}
var fechaValor = data.data[0];
var value = fechaValor[1];
Logger.log(value);
return value;
}
function fetchValue(key, today) {
var url = "https://apis.datos.gob.ar/series/api/series/?limit=1&start_date=";
url = url.concat(today.getYear(), "-", today.getMonth() + 1, "-",
today.getDate(), "&ids=", key, "&format=json&metadata=none");
var response = UrlFetchApp.fetch(url);
return JSON.parse(response.getContentText());
}