0

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;
    
    }

1 Answer 1

3

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.

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

1 Comment

Thank you! I actually tried that method but I was using the wrong syntax. It works now.

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.