0

I am trying to implement caching. I've written code in bounded script of spreadsheet. It's working fine i.e. I am able to get values against particular key. But this code is valid only for bounded script.

Does anyone know that how to access value against particular key from separate script?

Code to put in cache:(Bounded Script)

var sp_key = '1231232';
var ss = SpreadsheetApp.openById(sp_Key); 
var s = ss.getSheetByName("test_Sheet");
var cache = CacheService.getScriptCache();
var val= "xyz"
cache.put(A, val);
var cache = CacheService.getPublicCache();
Logger.log(cache.get(A));

Above code works fine. But if I want to get the value from unbounded script then what is the best way?

1 Answer 1

2

The getScriptCache() method also works in a stand alone Apps Script project.

There is an error in your code. A is not defined. Either put quotes around A or define A as a variable, and assign a value

function scriptCache() {
  var sp_key = '1231232';
  //var ss = SpreadsheetApp.openById(sp_Key); 
  //var s = ss.getSheetByName("test_Sheet");
  var cache = CacheService.getScriptCache();
  var val= "xyz"
  cache.put('A', val);
  var cache = CacheService.getPublicCache();
  Logger.log(cache.get('A'));
}

I ran that code in a stand alone Apps Script and it works.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Actually if I put values from bounded script code.gs in cache and there is one more script say getCacheValue.gs to get values from cache. Then how to do this?
If the code is inside of the same project, then you get the Cache the same way that it's done in that code. Setting and Getting the Cache does not need to be inside of the same function. Some programming languages require a statement to include the code from a separate code file, but Apps Script automatically shares all code from all code files in the entire project. If that's what you are asking.
Actually I am asking accessing cache of one project file in another project file.
I've never tried getting cache of another project, but I'm sure that it can not be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.