Когда я попытался вызвать hello()
в своих таблицах Google, он вернул
Ошибка TypeError: Не удается прочитать свойство '5. Обменный курс» не определено (строка 50).
Также настроен триггер для скрипта приложений
Criteria | set |
---|---|
Choose which function to run | hello |
Which runs at deployment | head |
Select event source | From spreadsheet |
Select event type | On Open |
Failure notification settings | Notify me Daily |
ОБЪЕКТ JSON
{
"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 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;
}
Обновлять
Я установил предопределенную переменную с именем jsonObj
, она может получить элемент в объекте и поместить его в листы Google.
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;
}