Reputation: 320
I have a WebApp which takes some values from a google sheet when it is opened. I want to store a global variable so that I do not have to make a call to the server all the time I need that variable.
I checked several questions online, but I am still not able to make it work.
Below is an extract of my code with a tentative to use PropertiesService. However it gives me an error that PropertiesService is not defined.
What is the easiest way to store a variable and use it in different functions from the client (html.file) side?
google.script.run.withSuccessHandler(yourCallBack2).getPNInfo(body);
}
}
function yourCallBack2(pinfo) {
console.log("callback called");
document.getElementById("ea1").textContent=pinfo[0];
document.getElementById("in1").value=1;
PropertiesService.getScriptProperties().setProperty('TEST', pinfo[0]);
}
document.getElementById("in1").addEventListener("change",updateQ);
function updateQ(){
var ea = PropertiesService.getScriptProperties().getProperty('TEST');
console.log(ea);
var eax = Number(ea);
var q = document.getElementById("in1").value;
document.getElementById("ea1").textContent=eax*q;
}
Upvotes: 0
Views: 1005
Reputation: 50445
The easiest way is store it as a global variable:
var TEST;//Declare global
google.script.run.withSuccessHandler(yourCallBack2).getPNInfo(body);
function yourCallBack2(pinfo) {
console.log("callback called");
TEST = pinfo[0];//set global
}
function updateQ(){
var ea = TEST;//get global
console.log(ea);
}
Other ways to persist information would be to use
google.script.run
is a async call. So, updateQ
might run before yourCallBack2
and your global variable TEST
may be undefined
by then.
Upvotes: 3