无法检索参数输出的值

时间:2019-02-14 06:31:53

标签: json google-apps-script google-sheets alphavantage

我正在Google表格中使用Programmatically launch a web app on the home screen? 从AlphaVantage API导入数据值。

例如,查询字符串: ImportJSON function

返回:

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-02-13 16:00:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2019-02-13 16:00:00": {
            "1. open": "106.8400",
            "2. high": "107.0000",
            "3. low": "106.7200",
            "4. close": "106.7800",
            "5. volume": "855430"
        },
        "2019-02-13 15:55:00": {
            "1. open": "106.9100",
            "2. high": "106.9350",
            "3. low": "106.7800",
            "4. close": "106.8300",
            "5. volume": "336088"
        },
        "2019-02-13 15:50:00": {
            "1. open": "107.0800",
            "2. high": "107.0821",
            "3. low": "106.8500",
            "4. close": "106.9300",
            "5. volume": "279939"
        }
    }
}

我想在Google表格单元格中显示最后更新的“收盘价”值。所以我在用这个查询:

IMPORTJSON("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=acc.ns&interval=1min&apikey=OJATWN8EM09KRWZS", "Time Series (1min)/4. close" )

但它显示

  

节点不可用”错误。

有人可以帮我解决我做错的事情吗?

1 个答案:

答案 0 :(得分:0)

OP可能会更轻松地只获取脚本的关键部分并避免使用 xpath 。这段代码将从传递给它的URL中获取json,并返回一个4. close值数组。使用数组,您可以将值附加到电子表格。这很容易找到如何处理一点Google-ing。

function getCloseDataFromURL(url){
  var res = UrlFetchApp.fetch(url); 
  var content = res.getContentText(); 

  var json = JSON.parse(content)["Time Series (5min)"]; //Just get Time Series section...
  var data = [];

  Object.keys(json).forEach(function(item){ //Get the timestamp keys...
  data.push(json[item]["4. close"]); //for each "4. close" item get the value and add it to an array
  });

  return data; //data is an array of the 4. close values.
}