Well, it looks like my problem was that google behaves in a weird way - it does not restart the script, as long as the script parameters are similar, it uses the cached results of previous runs. Therefore, it does not reconnect to the API and doesnβt retrieve the price; it simply returns the previous result of the script that was cached.
See here for more details: https://code.google.com/p/google-apps-script-issues/issues/detail?id=888
and here: Script to summarize data not updated
My solution was to add another parameter to my script, which I don't even use. Now, when you call a function with a parameter different from previous calls, it will have to restart the script, because the result for these parameters will not be in the cache.
Therefore, whenever I call a function, for an additional parameter, I pass "$ A $ 1". I also created a refresh menu item, and when I ran it, it puts the current date and time in A1, so all calls in the script with $ A $ 1 as the second parameter will have to be recounted. Here is some code from my script:
function onOpen() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); var entries = [{ name : "Refresh", functionName : "refreshLastUpdate" }]; sheet.addMenu("Refresh", entries); }; function refreshLastUpdate() { SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue(new Date().toTimeString()); } function getPrice(itemId, datetime) { var headers = { "method" : "get", "contentType" : "application/json", headers : {'Cache-Control' : 'max-age=0'} }; var jsonResponse = UrlFetchApp.fetch("http://someURL?item_id=" + itemId, headers); var jsonObj = eval( '(' + jsonResponse + ')' ); return jsonObj.Price; SpreadsheetApp.flush(); }
And when I want to put the price of the element with id 5 in the cell, I use the following formula:
=getPrice(5, $A$1)
When I want to update prices, I just click on the menu item "Refresh" β "Refresh". Remember that you need to reload the table after changing the onOpen() script.
tbkn23 Jun 27 '13 at 15:30 2013-06-27 15:30
source share