在Google Apps脚本中获取多个api请求,解析一次

问题描述 投票:1回答:1

我在使用UrlFetchApp的fetchAll方法时遇到问题。

我目前正在运行多个函数来从少量链接中收集api数据,并且api操作略有不同。所有函数都在一致的JSON字段下返回和解析数据。我想有一个函数一次检索api数据,解析一次,然后输出一次。我认为fetchAll会是更好的方法。输出将是电子表格。

以下是为简化而简化的脚本的变体。我一直在第14行找到“无法找到方法fetchAll(object,object)”错误。我尝试使用UrlFetchApp.fetchAll [eth,btc]获取网址,这让我超越了错误但是“无法调用方法'getContentText'未定义的“。

似乎无法从Google Apps documentation找出正确的方法。到目前为止,谷歌讨论组没有帮助。

我是否需要将解析作为fetchAll方法中的对象合并?

function dailyhx() {
  var ss = SpreadsheetApp.getActive().getSheetByName('DailyHx');  
  var eth = {
    'url' : 'https://min-api.cryptocompare.com/data/histoday?fsym=ETH&tsym=USD&limit=10',
    'method' : 'get',
    'contentType' : 'application/json',
  };
  var btc = {
    'url' : "https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=10",
    'method' : 'get',
    'contentType' : 'application/json',
  };
  var mph = UrlFetchApp.fetchAll(eth, btc)
  var j1 = JSON.parse(mph.getContentText());
  Loager.log(j1)
}
json google-apps-script google-sheets
1个回答
3
投票

UrlFetchApp.fetchAll()函数只需要一个必须是数组的参数。

所以替换这个:

var mph = UrlFetchApp.fetchAll(eth, btc);

有了这个:

var requests = [eth, btc];
var mph = UrlFetchApp.fetchAll(requests);
© www.soinside.com 2019 - 2024. All rights reserved.