-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-sheets-cryptocurrency-portfolio.js
More file actions
204 lines (170 loc) · 5.05 KB
/
Copy pathgoogle-sheets-cryptocurrency-portfolio.js
File metadata and controls
204 lines (170 loc) · 5.05 KB
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
var portfolioSheetName = "Portfolio";
var historySheetName = "History";
function UPDATE() {
var sheet = getSheetWithName(portfolioSheetName);
range = sheet.getRange(3, 1, 16, 1);
coins = range.getDisplayValues();
// get secondary currency.
range2 = sheet.getRange(2, 7, 1, 1);
currency2 = range2.getDisplayValue();
lastIdx = 0;
Logger.log(range.getDisplayValues());
for (var idx in coins) {
coin = coins[idx][0];
if (coin.length == 0) continue;
coin = coin.trim().toLowerCase();
coin = coin.replace(/ /g, "-");
Logger.log(coin);
if (coin == "-") {
break;
}
r = getCoinMarketCapPrice(coin, currency2);
outRange = sheet.getRange(3 + parseInt(idx), 2, 1, r.length);
outRange.setValues([r]);
lastIdx = parseInt(idx);
}
outRange = sheet.getRange(3 + parseInt(lastIdx) + 4, 2, 1, 1);
outRange.setValues([["Updated: " + getDateTimeString()]]);
}
function RECORD_HISTORY() {
var historySheet = getSheetWithName(historySheetName);
if (historySheet == null) {
historySheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(
historySheetName,
1
);
}
setupHistoryHeader(historySheet);
range = historySheet.getRange(2, 1, 1, 100);
var values = range.getValues();
values[0][0] = new Date();
historySheet.appendRow(values[0]);
}
function getSheetWithName(name) {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var idx in sheets) {
if (sheets[idx].getName() == name) {
return sheets[idx];
}
}
return null;
}
function getCoinMarketCapPrice(coin, convertCurrency) {
var url =
"https://api.coinmarketcap.com/v1/ticker/" +
coin +
"?convert=" +
convertCurrency;
var response = UrlFetchApp.fetch(url);
var json = JSON.parse(response.getContentText());
symbol = json[0]["symbol"];
btc = parseFloat(json[0]["price_btc"]);
usd = parseFloat(json[0]["price_usd"]);
krw = parseFloat(json[0]["price_" + convertCurrency.toLowerCase()]);
change_1h = parseFloat(json[0]["percent_change_1h"]) + "%";
change_1d = parseFloat(json[0]["percent_change_24h"]) + "%";
change_7d = parseFloat(json[0]["percent_change_7d"]) + "%";
rank = parseInt(json[0]["rank"]);
return [change_1h, change_1d, change_7d, btc, usd, krw];
}
function getDateTimeString(timestamp) {
var date = new Date();
if (0) {
// Korean format
var month = date.getUTCMonth() + 1;
var day = date.getUTCDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var formattedTime =
month +
"월 " +
day +
"일" +
" " +
hours +
":" +
minutes.substr(-2) +
":" +
seconds.substr(-2);
return formattedTime;
}
return String(date);
}
function setupHistoryHeader(hsheet) {
range = hsheet.getRange(1, 1, 1, 200);
arr = range.getDisplayValues();
//Logger.log(arr[0][0]);
header = ["DATE", "USD TOTAL"];
for (var idx in arr[0]) {
h = arr[0][idx];
if (idx < 2) continue;
if (h.length == 0) break;
if (h == "-") {
break;
}
header.push(h);
}
var sheet = getSheetWithName(portfolioSheetName);
{
// 1. find new symbol and add to new header (out)
range = sheet.getRange(3, 1, 100, 2);
coins = range.getDisplayValues();
for (var idx in coins) {
coin = coins[idx][0];
if (coin.length == 0) continue;
if (coin == "-") {
break;
}
coinSymbol = coins[idx][1];
if (header.indexOf(coinSymbol) == -1) {
header.push(coinSymbol);
}
//Logger.log(coinSymbol);
}
}
header.push("-");
//Logger.log(header);
outRange = hsheet.getRange(1, 1, 1, header.length);
outRange.setValues([header]);
outRange = hsheet.getRange(4, 1, 1, header.length);
outRange.setValues([header]);
outRange = hsheet.getRange(2, 1, 1, 1);
outRange.setValues([["*CURRENT*"]]);
{
// 2. find USD (out)
range = sheet.getRange(2, 1, 1, 100);
arr = range.getDisplayValues();
c = arr[0].indexOf("Rank");
if (c == -1) {
c = arr[0].indexOf("24h");
}
usdIdx = arr[0].indexOf("USD", c) + 1;
// 3. find new symbol and add to new header (out)
range = sheet.getRange(3, 1, 100, 2);
coins = range.getDisplayValues();
//Logger.log(coins);
for (var idx in coins) {
coin = coins[idx][0];
if (coin.length == 0) continue;
if (coin == "-") {
break;
}
coinSymbol = coins[idx][1];
//Logger.log(coinSymbol);
headerIdx = header.indexOf(coinSymbol);
if (headerIdx == -1) {
Logger.log("WHAT???");
}
range2 = sheet.getRange(3 + parseInt(idx), usdIdx, 1, 1);
a1n = range2.getA1Notation();
outRange = hsheet.getRange(2, headerIdx + 1, 1, 1);
outRange.setFormula("=" + portfolioSheetName + "!" + a1n);
lastIdx = parseInt(idx);
}
range2 = sheet.getRange(3 + parseInt(lastIdx) + 2, usdIdx, 1, 1);
a1n = range2.getA1Notation();
outRange = hsheet.getRange(2, 2, 1, 1);
outRange.setFormula("=" + portfolioSheetName + "!" + a1n);
}
}