I am trying to create array from an API output. And then filter the array down to one item designated by the itemID. Below is my attempt at it. I am getting an error that jsonArray isn't present in function test. I presume I need to make it a global variable or something, but that is where I hit a wall.
function onOpen() {
var myUrl = "http://www.gw2spidy.com/api/v0.9/json/all-items/all"
var jsonData = UrlFetchApp.fetch(myUrl);
var jsonArray = JSON.Parse(jsonData);
return jsonArray
}
function test(itemID) {
var jsonFilter = jsonArray.filter(function(itemID){return itemID.data_id==itemID});
var jsonObject = JSON.parse(jsonFilter).result;
var adjustedValue = (jsonObject.min_sale_unit_price / 100);
return adjustedValue;
}
I previously had it working with the following code (ripped from someone else), but each use of that function made a call. I am trying to reduce the number of calls to one call per sheet refresh (this is in the google docs script manager).
// function to return the current sell value for an item (designated by
// the item’s ID number) from GW2Spidy's API formatted with copper in
// the tens and hundreds places
function getItemSellValue(itemID) {
// base URL for the API that will return JSON for the itemID supplied
var myUrl = "http://www.gw2spidy.com/api/v0.9/json/item/" + escape(itemID);
// fetches the information from the URL in an HTTPResponse
var jsonData = UrlFetchApp.fetch(myUrl);
// now we convert that response into a string so that we can use it
var jsonString = jsonData.getContentText();
// now, we turn it into a Javascript object for easier handling
// *note: I also remove the "result" wrapper object so we can
// use direct calls on the objects parameters
var jsonObject = JSON.parse(jsonString).result;
// now I divide the value by 100 in order to format it more like
// currency. (ie. 126454, which equals 12 gold, 64 silver,
// and 54 copper will now be displayed as
// 1264.54)
var adjustedValue = (jsonObject.min_sale_unit_price / 100);
// finally we return the adjusted min sell value
return adjustedValue;
}
Update
I updated the code dropping the onOpen() and switching to the cache service. I am receiving the following error now: "error: Argument too large: value (line 12, file "gwspidy api")". Line 12 is cache.put("gw2spidy-data", jsonData, 1500);
Is it just the size of the data? I don't know how much I can filter it down. The full code is below.
function test(itemID) {
var cache = CacheService.getPublicCache();
var cached = cache.get("gw2spidy-data");
// check if the data is cached and use it if it is
if (cached != null) {
var jsonData = cached
}
//otherwise fetch the data and cache it
else {
var myUrl = "http://www.gw2spidy.com/api/v0.9/json/all-items/all"
var jsonData = UrlFetchApp.fetch(myUrl);
cache.put("rss-feed-contents", jsonData, 1500);
}
//put data into an array and filter the result down to the given id, then return the function value
var jsonArray = JSON.Parse(jsonData);
var jsonFilter = jsonArray.filter(function(itemID){return itemID.data_id==itemID});
var jsonObject = JSON.parse(jsonFilter).result;
var adjustedValue = (jsonObject.min_sale_unit_price / 100);
return adjustedValue;
}