简体   繁体   中英

google web app script - global variable on client side

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM