如何将变量从客户端传递到服务器并将其保留在服务器中以用于其他功能

时间:2022-01-26 04:56:23

In my google appscripts web app, I have a variable defined in the client (js) file. Let's call that variable qty. I need that variable in a number of server functions, so currently I'm doing this:

在我的谷歌appscripts Web应用程序中,我在客户端(js)文件中定义了一个变量。我们称之为变量数量。我需要在许多服务器函数中使用该变量,所以目前我正在这样做:

<script>
google.script.run.serverFunction(qty);
</script>

And my server script looks like this:

我的服务器脚本如下所示:

function serverFunction(qty) {
      ... 
      INSERT INTO table (blah) VALUES (qty);
      ...
    }

But instead, I want to pass qty to server once, set it as a global variable (?) ... so that I can use it directly in all my server functions and not have to pass it from the client every time.

但相反,我想将qty传递给服务器一次,将其设置为全局变量(?)...以便我可以在所有服务器函数中直接使用它,而不必每次都从客户端传递它。

Why do I need this? Because currently, a savvy user can run google.script.run.serverFunction(qty); in the console and INSERT random things. Figure if the input variable is set in server, there's more control.

我为什么需要这个?因为目前,精明的用户可以运行google.script.run.serverFunction(qty);在控制台和INSERT随机的东西。如果输入变量是在服务器中设置的,那么就有更多的控制权。

1 个解决方案

#1


2  

You will need to use:

你需要使用:

PropertiesService.getScriptProperties() 

and put the data into the Properties Service. However, one property can only hold 9k of data. If your data is over 9k, you'll need to split it up. If it's a lot more than 9k, like 100k, then it might not work to put data into the Properties Service. If you only need to hold the data for less than 6 hours, you can use Cache Service, and store up to 100k in one property.

并将数据放入Properties Service。但是,一个属性只能容纳9k的数据。如果您的数据超过9k,则需要将其拆分。如果它超过9k,如100k,那么将数据放入Properties Service可能无效。如果您只需要保存数据的时间少于6小时,则可以使用缓存服务,并在一个属性中存储最多100k。

Apps Script Documentation - Cache Service

应用脚本文档 - 缓存服务

#1


2  

You will need to use:

你需要使用:

PropertiesService.getScriptProperties() 

and put the data into the Properties Service. However, one property can only hold 9k of data. If your data is over 9k, you'll need to split it up. If it's a lot more than 9k, like 100k, then it might not work to put data into the Properties Service. If you only need to hold the data for less than 6 hours, you can use Cache Service, and store up to 100k in one property.

并将数据放入Properties Service。但是,一个属性只能容纳9k的数据。如果您的数据超过9k,则需要将其拆分。如果它超过9k,如100k,那么将数据放入Properties Service可能无效。如果您只需要保存数据的时间少于6小时,则可以使用缓存服务,并在一个属性中存储最多100k。

Apps Script Documentation - Cache Service

应用脚本文档 - 缓存服务