-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmc.gs
394 lines (289 loc) · 9.07 KB
/
cmc.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Installing the add-on.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Called when document gets opened.
*/
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createAddonMenu()
.addItem("Get CMC Data", "menuGetCmcData")
.addToUi();
}
// cmc api url
var cmcUrl = "https://api.coinmarketcap.com/v1/ticker/";
// key value store for curencies {id: symbol}
var documentProperties = PropertiesService.getDocumentProperties();
/**
* Creates a new spreadsheet "CMC" which holds all coins information.
*/
function menuGetCmcData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetName = "CMC";
const sheet = ss.getSheetByName(sheetName) || ss.insertSheet(sheetName);
sheet.clear();
var data = getCmcData(true, 0);
function rangeClosure(a,b,row,idx) {
return function() {
const range = sheet.getRange(a, b);
var col = row[idx];
// make header bold
if (a === 1) {
range.setFontWeight("bold");
}
// color columns "1h Change", "24h Change", "7d Change"
if ([9, 10, 11].indexOf(idx) > -1 && a !== 1) {
if (col >= -0.05 && col <= 0.05) {
range.setBackground("yellow");
} else if (col < -0.05 && col >= -5) {
range.setBackground("lightcoral");
} else if (col < -5) {
range.setBackground("indianred");
} else if (col > 0.05 && col <= 5) {
range.setBackground("palegreen");
} else {
range.setBackground("limegreen");
}
}
// translate last updated timestamp to utc date
if (idx === 12 && a !== 1) {
const d = new Date(col*1000);
const dateString =
d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2) + " " +
d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
col = dateString;
}
range.setValue(col);
};
}
if (data && data.map) {
data.map(function(row, i) {
for (var j=0; j<row.length; j++) {
var k = i+1;
var l = j+1;
rangeClosure(k,l,row,j)();
}
});
}
// freeze first row
sheet.setFrozenRows(1);
}
/**
* Store the id symbol mapping in persistent document key value store.
*
* @param {object} data The data to be stored.
*/
function storeIdSymbolMapping(data) {
if (data) {
documentProperties.setProperties(data);
}
}
/**
* CMC api helper for currency access.
*/
function cmcApiCurrency(currency, convert) {
var params = "";
if (!currency) {
throw "No currency specified.";
}
if (convert) {
params = "/?convert="+convert;
}
const response = UrlFetchApp.fetch(cmcUrl + currency + params, {'muteHttpExceptions': true});
return JSON.parse(response.getContentText());
}
/**
* CMC api helper for list access.
*/
function cmcApiTicker(limit, convert, start) {
var params = [];
var paramsString = "";
if (limit !== undefined) {
params.push("limit="+limit);
}
if (start) {
params.push("start="+start);
}
if (convert) {
params.push("convert="+convert);
}
if (params.length > 0) {
paramsString = "?" + params.join("&");
}
const response = UrlFetchApp.fetch(cmcUrl + paramsString, {'muteHttpExceptions': true});
return JSON.parse(response.getContentText());
}
/**
* Helper for retrieving the coin data list from CMC to be used in a sheet directly.
*
* @param {"true|false"} storeData Indicates if the data should be persisted in documentProperties storage.
* @param {"100"} limit The limit of items on the list.
* @param {"EUR"} convert Type in a currency to also get the conversion rate of that currency for all currencies on the list.
* @param {"50"} start The currency rank where the list will start.
*/
function getCmcData(storeData, limit, convert, start) {
// field mapping for cmc api request
var fields = {
"name": "Name",
"symbol": "Symbol",
"rank": "Rank",
"price_usd": "Price in $",
"price_btc": "Price in BTC",
"market_cap_usd": "Market Cap in $",
"available_supply": "Available Supply",
"total_supply": "Total Supply",
"max_supply": "Max Supply",
"percent_change_1h": "1h Change",
"percent_change_24h": "24h Change",
"percent_change_7d": "7d Change",
"last_updated": "Last updated",
};
if (convert) {
fields["price_" + convert.toLowerCase()] = "Price in " + convert;
}
const content = cmcApiTicker(limit, convert, start);
var cmcData = [];
var idSymbolMapping = {};
// create the head
var head = [];
for (var f in fields) {
head.push(fields[f]);
}
cmcData.push(head);
// push obj values
if (content.map) {
content.map(function(obj) {
var row = [];
for (var f in fields) {
if (obj[f]) {
row.push(obj[f]);
} else {
row.push(null);
}
}
cmcData.push(row);
if (storeData) {
idSymbolMapping[obj.id] = obj.symbol
}
});
if (storeData && idSymbolMapping) {
storeIdSymbolMapping(idSymbolMapping);
}
}
return cmcData;
}
var allowedAttrs = ["name", "symbol", "rank", "price", "marketcap", "supply", "maxsupply", "1hchange", "24hchange", "7dchange", "updated"];
/**
* Maps user defined attributes to the object that will be retrieved by an api call.
*/
function mapAttr(attr, currencies) {
var result = {};
const attrArray = attr.split(',');
const price = "price_" + currencies.convert.toLowerCase();
const attrMap = {
"name": "name",
"symbol": "symbol",
"rank": "rank",
"price": price,
"marketcap": "market_cap_usd",
"supply": "available_supply",
"maxsupply": "max_supply",
"1hchange": "percent_change_1h",
"24hchange": "percent_change_24h",
"7dchange": "percent_change_7d",
"updated": "last_updated"
};
attrArray.forEach(function(a) {
// validate attributes
if (allowedAttrs.indexOf(a) === -1) {
throw "Invalid attribute(s).";
}
result[a] = attrMap[a];
});
return result;
}
/**
* Takes a string in the form of e.g. "BTC/EUR" and transforms in into an object with the following structure:
* {
* base: {
* id: id,
* symbol: symbol
*` },
* convert: convertSymbol
* }
*
* @param {"BTC/EUR"} currencyPair
*/
function resolveCurrencyPair(currencyPair) {
var result = {};
const all = documentProperties.getProperties();
const currencies = currencyPair.split("/");
if (currencies.length === 1) {
result.convert = "USD";
} else {
result.convert = currencies[1];
}
if (!all) {
throw "Please update CMC data via menu: CMC -> Get CMC Data"
}
for (id in all) {
if (all[id] === currencies[0]) {
result.base = {"id": id, "symbol": all[id]};
return result;
}
}
throw "Invalid base currency. Maybe you need to update CMC data? Go to menu CMC -> Get CMC Data.";
}
/**
* Get currency data from CMC. If called without any parameters, function will return default currency pair (BTC/USD)'s price.
*
* @param {"BTC/USD"} currencyPair A currency pair. The first is the base currency which should get converted into the second currency.
* @param {"price,1hchange,rank"} attr The attributes to retrieve. Default is price. Possible attr's are: name, symbol, rank, price, marketcap, supply, maxsupply, 1hchange, 24hchange, 7dchange, updated
* @customfunction
*/
function CMC(currencyPair, attr) {
var currencies;
if (currencyPair !== undefined) {
currencies = resolveCurrencyPair(currencyPair);
} else {
currencies = {base: {id: "bitcoin", symbol: "BTC"}, convert: "USD"};
}
if (currencies) {
var mappedAttr;
if (attr) {
mappedAttr = mapAttr(attr, currencies);
} else {
mappedAttr = mapAttr("price", currencies);
}
const content = cmcApiCurrency(currencies.base.id, currencies.convert)[0];
var result = [];
for (attr in mappedAttr) {
result.push(content[mappedAttr[attr]]);
}
return [result];
}
}
/**
* Get list data from CMC. If called without any parameters, CMCLIST will return the top 100 currencies.
*
* @param {"100"} limit The limit of items on the list. Put in "0" to retrieve all currencies. Default is 100.
* @param {"EUR"} convert Type in a currency to also get the conversion rate of that currency for all currencies on the list. Default is USD.
* @param {"50"} start The currency rank where the list will start (in combination with limit, which represents the end, where last index = start + limit). Leave empty for starting from beginning.
* @customfunction
*/
function CMCLIST(limit, convert, start) {
if (limit !== undefined) {
if (convert) {
if (start) {
return getCmcData(false, limit, convert, start);
}
return getCmcData(false, limit, convert);
}
return getCmcData(false, limit);
} else {
return getCmcData(false, 100);
}
}