I am trying to save some user input from a Google App Script form on a spreadsheet to the private cache.
Here is a test script:
var cache = CacheService.getPrivateCache();
function onLoad() {
var sheet = SpreadsheetApp.getActiveSpreadsheet(),
entries = [{
name : "Show form",
functionName : "showForm"
}];
sheet.addMenu("Test Menu", entries);
}
function showForm() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
app = UiApp.createApplication(),
setButton = app.createButton("Set"),
setHandler = app.createServerClickHandler('setTest');
setButton.addClickHandler(setHandler);
app.add(setButton);
spreadsheet.show(app);
}
function setTest(event) {
cache.put("test", "test", 7200);
Browser.msgBox("Test was set: " + cache.get("test") + ". Use the getTest cell formula to test the cache.");
}
function getTest() {
var result = cache.get("test");
return result;
}
Once I click the menu button, a form appears and sets a cache value in a server handler. Then I try to get the value from the cache using =getTest() in a cell. I would expect the cache to return the value “test” but it seems to return null.
I started this issue here:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=2039
And I also found another similar one:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1804
Trying to save some user input on a form into the cache and be able to access it from another function at a later time.
Any suggestions?
Custom functions have limited scope and then can only access a few select services. You read more about their limitations here.
With Cache its a bit tricky – caching is per “script scope”. So when a Cache is accessed first from the Server handler script scope its running as a UiApp which has more privileges and that is different from the script scope that a custom function runs as. Therefore, the cache is not shared between those two scopes.
You can access cached items that you set in custom functions from other custom functions, but this cache can’t cross these boundaries.
You could theoretically store this in ScripProperties but that can very clumsy and it would be misusing those capabilities and this is shared amongst all users.
and
If you can explain your usecase a bit more in depth, perhaps we can provide some alternative solutions.