google apps script - JSON Array Creation and Filtering -
i trying create array api output. , filter array down 1 item designated itemid. below attempt @ it. getting error jsonarray isn't present in function test. presume need make global variable or something, hit 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 had working following code (ripped else), each use of function made call. trying reduce number of calls 1 call per sheet refresh (this in google docs script manager).
// function return current sell value item (designated // item’s id number) gw2spidy's api formatted copper in // tens , hundreds places function getitemsellvalue(itemid) { // base url api return json itemid supplied var myurl = "http://www.gw2spidy.com/api/v0.9/json/item/" + escape(itemid); // fetches information url in httpresponse var jsondata = urlfetchapp.fetch(myurl); // convert response string can use var jsonstring = jsondata.getcontenttext(); // now, turn javascript object easier handling // *note: remove "result" wrapper object can // use direct calls on objects parameters var jsonobject = json.parse(jsonstring).result; // divide value 100 in order format more // currency. (ie. 126454, equals 12 gold, 64 silver, // , 54 copper displayed // 1264.54) var adjustedvalue = (jsonobject.min_sale_unit_price / 100); // return adjusted min sell value return adjustedvalue; }
update updated code dropping onopen() , switching cache service. receiving following error now: "error: argument large: value (line 12, file "gwspidy api")". line 12 cache.put("gw2spidy-data", jsondata, 1500);
size of data? don't know how can filter down. full code below.
function test(itemid) { var cache = cacheservice.getpubliccache(); var cached = cache.get("gw2spidy-data"); // check if data cached , use if if (cached != null) { var jsondata = cached } //otherwise fetch data , cache 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 array , filter result down given id, return 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; }
it looks you've got few ideas crossing over, causing problems.
the object
jsonarray
declared within scope of functiononopen()
, , out of scopetest()
.an
onopen()
trigger function simple trigger, when in spreadsheet or document container-bound script. it's not clear question or code whether script in 1 or other, or standalone script.an
onopen()
function doesn't needreturn
- event manager invokes ignore value returned. isn't mechanism making value available outside scope of function.if intent have
onopen()
function populate object can used other functions, you're right think global1... instead need use other mechanism share value. (look cache service - perfect this.)
1 each separate execution of script done in new execution instance. variables defined outside of block of code (aka "global" variables) therefore unique instance. when trigger function invoked event, runs in own instance, , global values sets visible instance; function invoked spreadsheet or document ui, example, have own version of non-scoped object (global).
Comments
Post a Comment