如何在Google Sheets中通过Bittrex API按MarketName获取指定数据
How to Retrieve Bittrex Market Data by MarketName (No Index Dependency)
Got it, let's tweak your script so you can grab the exact BTC-RCN market data without relying on array indexes. The core idea is to filter the API's result array to match your target MarketName directly. Here's the modified version:
Updated Script
function Bittrex_API() { // Call the Bittrex API for market summaries var response = UrlFetchApp.fetch("https://bittrex.com/api/v1.1/public/getmarketsummaries"); // Parse the JSON response var json = response.getContentText(); var data = JSON.parse(json); // First, confirm the API call succeeded (Bittrex returns a success flag) if (data.success) { // Use Array.find() to locate the market with MarketName "BTC-RCN" var btcRcnMarket = data.result.find(function(market) { return market.MarketName === "BTC-RCN"; }); if (btcRcnMarket) { // Log the full set of details for BTC-RCN Logger.log("BTC-RCN Market Data:"); Logger.log(btcRcnMarket); // Access specific fields directly if you need them Logger.log("Last Trade Price: " + btcRcnMarket.Last); Logger.log("24-Hour Trading Volume: " + btcRcnMarket.Volume); Logger.log("Current Highest Bid: " + btcRcnMarket.Bid); } else { Logger.log("Error: Market BTC-RCN wasn't found in the API response."); } } else { // Handle API errors (like rate limits or service downtime) Logger.log("API Request Failed: " + data.message); } }
Key Changes Breakdown
- Validate API Success: Bittrex's API includes a
successboolean in its response. We check this first to avoid processing invalid or error data, which prevents unexpected script crashes. Array.find()for Targeted Lookup: Instead of using a fixed index likedata.result[0], we usefind()to loop through theresultarray and return the first item whereMarketNameexactly matches "BTC-RCN". This is reliable even if the order of markets in the API response changes over time.- Direct Field Access: Once you have the
btcRcnMarketobject, you can pull specific properties (like price, volume, bids) directly without sifting through the entire array.
Bonus: Handling Multiple Matches (If Needed)
If you ever want to fetch a group of markets (e.g., all BTC pairs), swap find() with filter()—this will return an array of all matching entries:
var btcMarkets = data.result.filter(function(market) { return market.MarketName.startsWith("BTC-"); }); Logger.log("All BTC-Based Markets:"); Logger.log(btcMarkets);
内容的提问来源于stack exchange,提问作者Chris Willson
相关产品推荐
相关产品推荐

