因此,我有来自 CoinMarketCap 的这个脚本来查询加密货币价格。我想修改这个脚本,以便可以将其应用于单元格,然后将其拖放到列中以读取另一个单元格列中的股票代码。我对 App Scripts 完全是菜鸟。
function getLatestBTCPrice() {
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest";
var apiKey = 'YOUR_API_KEY'; // Replace with your API key
var headers = {
"X-CMC_PRO_API_KEY": apiKey,
"Accept": "application/json"
};
var parameters = {
"symbol": "BTC"
};
var response = UrlFetchApp.fetch(url + "?" + Object.keys(parameters).map(key => key + '=' +
parameters[key]).join('&'), {'headers': headers});
var json = JSON.parse(response.getContentText());
var btcPrice = json.data.BTC.quote.USD.price;
return btcPrice;
}
这是我迄今为止尝试过的方法,但没有成功。我遇到的问题是,var tickerPrice = json.data.ETC.quote.USD.price;
我想让 ETC 更新,因为它读取了新的股票代码,但我似乎无法弄清楚。
function getTickerPrice() {
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest";
var apiKey = 'YOUR_API_KEY'; // Replace with your API key
var headers = {
"X-CMC_PRO_API_KEY": apiKey,
"Accept": "application/json"
};
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var ticker = sheet.getRange('A3').getValue();
var parameters = {
"symbol": ticker
};
var response = UrlFetchApp.fetch(url + "?" + Object.keys(parameters).map(key => key + '=' +
parameters[key]).join('&'), {'headers': headers});
var json = JSON.parse(response.getContentText());
var tickerPrice = json.data.ETC.quote.USD.price;
return tickerPrice;