Skip to content

Instantly share code, notes, and snippets.

@m-cakir
Last active January 23, 2023 11:51
Show Gist options
  • Save m-cakir/4120dbf562cf7e0855dd27e07d46e1a4 to your computer and use it in GitHub Desktop.
Save m-cakir/4120dbf562cf7e0855dd27e07d46e1a4 to your computer and use it in GitHub Desktop.
Google Sheets - App Script - Binance Prices
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);
}
COLUMN A (token symbol) COLUMN B (USDT/BUSD) COLUMN C (price)
BTC USDT 0
ETH USDT 0
XRP BUSD 0
@m-cakir
Copy link
Author

m-cakir commented Jan 23, 2023

Steps

  • Extensions > App Scripts on Google Sheet
  • write code on App Script Editor
  • set Trigger if you want

Automation with Google Apps Script Triggers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment