You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 success boolean 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 like data.result[0], we use find() to loop through the result array and return the first item where MarketName exactly 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 btcRcnMarket object, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:48:08