简体   繁体   English

谷歌应用脚本访问第三级 JSON object

[英]Google App Script accessing third level JSON object

When I tried calling hello() on my Google Sheets, it return当我尝试在我的 Google 表格上调用hello()时,它返回

Error TypeError: Cannot read property '5.错误类型错误:无法读取属性 '5。 Exchange Rate' of undefined (line 50).未定义的汇率'(第 50 行)。

Trigger for the Apps Script is also set up Apps 脚本的触发器也已设置

Criteria标准 set
Choose which function to run选择要运行的 function hello你好
Which runs at deployment在部署时运行 head
Select event source Select 事件源 From spreadsheet从电子表格
Select event type Select 事件类型 On Open打开时
Failure notification settings失败通知设置 Notify me Daily每天通知我

JSON OBJECT JSON OBJECT

{
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "SGD",
        "4. To_Currency Name": "Singapore Dollar",
        "5. Exchange Rate": "1.32783000",
        "6. Last Refreshed": "2021-02-26 01:28:59",
        "7. Time Zone": "UTC",
        "8. Bid Price": "1.32783000",
        "9. Ask Price": "1.32783000"
    }
}

Google App Script function谷歌应用脚本 function

function hello(){
  var usdCurrency = "USD";
  var AV_API_Key = "KEYS";
  var sgdCurrency = "SGD"
  var url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=" + usdCurrency + "&to_currency=" + sgdCurrency  + "&apikey=" + AV_API_Key;

  var response =  UrlFetchApp.fetch(url);
  var result =  JSON.parse(response.getContentText());
  var currencyPrice = result["Realtime Currency Exchange Rate"]["5. Exchange Rate"];
  
  Logger.log(currencyPrice); // 1.32794000
  Logger.log(typeof(currencyPrice)); // String
  return currencyPrice;
  
}

Update更新

I set a predefined variable named jsonObj , it is able get the item in the object and place it in the google sheets.我设置了一个名为jsonObj的预定义变量,它能够获取 object 中的项目并将其放在谷歌表格中。


function initTesting(){
  var jsonObj = {
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "SGD",
        "4. To_Currency Name": "Singapore Dollar",
        "5. Exchange Rate": "1.32783000",
        "6. Last Refreshed": "2021-02-26 01:28:59",
        "7. Time Zone": "UTC",
        "8. Bid Price": "1.32783000",
        "9. Ask Price": "1.32783000"
    }
};
var getPrice = jsonObj["Realtime Currency Exchange Rate"]["5. Exchange Rate"];
return getPrice;
}

I am not sure whether it is a bug or something but I have work around a solution.我不确定这是一个错误还是什么,但我有一个解决方案。

function hello(){
  var usdCurrency = "USD";
  var AV_API_Key = "KEYS";
  var sgdCurrency = "SGD"
  var url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=" + usdCurrency + "&to_currency=" + sgdCurrency  + "&apikey=" + AV_API_Key;

  var response =  UrlFetchApp.fetch(url);
  var result =  JSON.parse(response.getContentText());
  var currencyPrice = result["Realtime Currency Exchange Rate"]["5. Exchange Rate"];

Even though when I initially logged Logger.log(typeof(currencyPrice));即使我最初记录Logger.log(typeof(currencyPrice)); it showed String on the log.它在日志上显示字符串。 But once I use a String() in the code below, it is able to reflect on the google sheets.但是一旦我在下面的代码中使用了String() ,它就能够反映在谷歌表格上。

  Logger.log(currencyPrice); // 1.32794000
  Logger.log(typeof(currencyPrice)); // String
  // uses  String() on the currency price
  stringCurrencyPrice = String(currencyPrice)
  return stringcurrencyPrice;
  
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM