Last active
January 23, 2023 11:51
-
-
Save m-cakir/4120dbf562cf7e0855dd27e07d46e1a4 to your computer and use it in GitHub Desktop.
Google Sheets - App Script - Binance Prices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myFunction() { | |
// https://api.binance.com/api/v3/ticker/price?symbols=%5B%22BTCUSDT%22,%22ETHUSDT%22%5D | |
const sheetStatus = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SHEET_NAME'); | |
const _rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues() | |
.filter(e => e[1] === 'USDT' || e[1] === 'BUSD') | |
.map((e, i) => { | |
// 0 = Column A | |
// 1 = Column B | |
return { index: i, symbol: e[0] + e[1] }; | |
}); | |
//console.log(_rows); | |
const rows = {}; | |
_rows.forEach(e => rows[e.symbol] = e.index); | |
//console.log(rows); | |
const apis = ['api', 'api1', 'api2', 'api3']; | |
const binanceUrl = `https://${apis[Math.floor(Math.random()*apis.length)]}.binance.com/api/v3/ticker/price?symbols=%5B${_rows.map(e => `%22${e.symbol}%22`).join(',')}%5D`; | |
//console.log(binanceUrl); | |
const prices = JSON.parse( | |
UrlFetchApp.fetch(binanceUrl, { | |
method: 'GET', | |
json: true, | |
gzip: true | |
}).getContentText() | |
); | |
prices.forEach(e => { | |
// 3 = Column C | |
const index = rows[e.symbol]; | |
const range = sheetStatus.getRange(index + 2, 3); | |
range.setNumberFormat("#.##0,00;(#.##0,00)"); | |
range.setValue(e.price.replace('.', ',')); | |
//console.log(`row.index: ${index}, row.price: ${e.price}`); | |
}); | |
//console.log(prices); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COLUMN A (token symbol) | COLUMN B (USDT/BUSD) | COLUMN C (price) | |
---|---|---|---|
BTC | USDT | 0 | |
ETH | USDT | 0 | |
XRP | BUSD | 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps
Automation with Google Apps Script Triggers