-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathporftolio-tracker.gs
149 lines (141 loc) · 4.57 KB
/
porftolio-tracker.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
/**
* CODE LICENSED UNDER THE CREATIVE COMMON BY-NC-ND LICENSE.
* https://creativecommons.org/licenses/by-nc-nd/4.0/
*
* Copyright 2021 by Baswazz
*/
/** @OnlyCurrentDoc */
const updateIntervalInHours = 1; // Hours
const currency = "EUR"; // USD
const apiKey = PropertiesService.getScriptProperties().getProperty("apiKey"); // Get your free API Key https://coinmarketcap.com/api/
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = SpreadsheetApp.getActiveSheet();
const sheetColSymbol = "B2:B"; // Currency symbol
const sheetColCoinName = "A";
const sheetColPrice = "D";
const sheetColPercentChange1h = "E";
const sheetColPercentChange24h = "F";
const sheetColPercentChange7d = "G";
const sheetColPercentChange30d = "H";
const sheetColPercentChange60d = "I";
const sheetColPercentChange90d = "J";
const sheetColMarketCap = "K";
const sheetColMarketCapDominance = "L";
const sheetColVolume24h = "M";
const sheetColVolumeChange24h = "N";
const symbols = sheet
.getRange(sheetColSymbol)
.getValues()
.flat()
.filter(Boolean);
function onOpen() {
// Add UI menu
SpreadsheetApp.getUi()
.createMenu("Crypto")
.addItem("Update", "fetchData")
.addItem("Install triggers", "createTimeDrivenTriggers")
.addToUi();
}
function fetchData() {
const headers = {
"X-CMC_PRO_API_KEY": apiKey,
Accept: "application/json",
};
const url =
"https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=" +
symbols.join(",") +
"&convert=" +
currency;
try {
const response = UrlFetchApp.fetch(url, { headers });
if (response.getResponseCode() === 200) {
const responseContent = response.getContentText();
const data = JSON.parse(responseContent);
dataToSheet(data);
} else {
Logger.log("Error: " + response.getResponseCode());
}
} catch (e) {
Logger.log("Exception: " + e.toString());
}
}
function dataToSheet(data) {
const coins = data.data;
for (const symbol in coins) {
if (coins.hasOwnProperty(symbol)) {
const coin = coins[symbol][0];
const rowIndex = symbols.indexOf(symbol) + 2; // Adding 2 to match sheet row index
// Write data to the corresponding row
if (sheetColCoinName) {
sheet.getRange(sheetColCoinName + rowIndex).setValue(coin.name);
}
if (sheetColPrice) {
sheet
.getRange(sheetColPrice + rowIndex)
.setValue(parseFloat(coin.quote[currency].price));
}
if (sheetColPercentChange1h) {
sheet
.getRange(sheetColPercentChange1h + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_1h) / 100);
}
if (sheetColPercentChange24h) {
sheet
.getRange(sheetColPercentChange24h + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_24h) / 100);
}
if (sheetColPercentChange7d) {
sheet
.getRange(sheetColPercentChange7d + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_7d) / 100);
}
if (sheetColPercentChange30d) {
sheet
.getRange(sheetColPercentChange30d + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_30d) / 100);
}
if (sheetColPercentChange60d) {
sheet
.getRange(sheetColPercentChange60d + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_60d) / 100);
}
if (sheetColPercentChange90d) {
sheet
.getRange(sheetColPercentChange90d + rowIndex)
.setValue(parseFloat(coin.quote[currency].percent_change_90d) / 100);
}
if (sheetColMarketCap) {
sheet
.getRange(sheetColMarketCap + rowIndex)
.setValue(parseFloat(coin.quote[currency].market_cap));
}
if (sheetColMarketCapDominance) {
sheet
.getRange(sheetColMarketCapDominance + rowIndex)
.setValue(
parseFloat(coin.quote[currency].market_cap_dominance) / 100
);
}
if (sheetColVolume24h) {
sheet
.getRange(sheetColVolume24h + rowIndex)
.setValue(parseFloat(coin.quote[currency].volume_24h));
}
if (sheetColVolumeChange24h) {
sheet
.getRange(sheetColVolumeChange24h + rowIndex)
.setValue(parseFloat(coin.quote[currency].volume_change_24h) / 100);
}
}
}
}
function createTimeDrivenTriggers() {
ScriptApp.newTrigger("fetchData")
.forSpreadsheet(spreadsheet)
.onOpen()
.create();
ScriptApp.newTrigger("fetchData")
.timeBased()
.everyHours(updateIntervalInHours)
.create();
}