Google Web应用程序脚本-客户端上的全局变量 [英] google web app script - global variable on client side

查看:57
本文介绍了Google Web应用程序脚本-客户端上的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebApp,它在打开时会从Google工作表中获取一些值.我想存储一个全局变量,这样就不必在需要该变量时一直对服务器进行调用.

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.

下面是我的代码摘录,并尝试使用PropertiesService.但是,这给了我一个错误,即未定义PropertiesService.

Below is an extract of my code with a tentative to use PropertiesService. However it gives me an error that PropertiesService is not defined.

从客户端(html.file)端存储变量并将其用于不同功能的最简单方法是什么?

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

  • Cookies
  • Session/Local storage

google.script.run异步调用.因此,updateQ可能在yourCallBack2之前运行,并且那时全局变量TEST可能是undefined.

google.script.run is a async call. So, updateQ might run before yourCallBack2 and your global variable TEST may be undefined by then.

这篇关于Google Web应用程序脚本-客户端上的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆