简体   繁体   English

在两个 HTML 页面之间传递数据(Google Apps 脚本)

[英]Pass data between two HTML pages (Google Apps Script)

I'm trying to pass var 'id' from the page 'a.html' to 'b.html'.我试图将 var 'id' 从页面 'a.html' 传递到 'b.html'。 The var content comes from 'code.gs' as below: var 内容来自“code.gs”,如下所示:

code.gs代码.gs

function data(){
var id = 1;

return id;
}

Next, I get this var and I show it in 'a.html':接下来,我得到这个 var 并在“a.html”中显示它:

a.html一个.html

<?
var id = data();
?>

<h1><?= id ?></h1>

<a href="b.html">Go to B.html</a>

By clicking 'Go to B.html', the system directs the user to there.通过单击“转到 B.html”,系统会将用户定向到那里。 I need to bring the same value of var 'id' from the page 'a.html' to 'b.html'.我需要将 var 'id' 的相同值从页面 'a.html' 带到 'b.html'。 Ps: searching for a little, I saw that there's a kind to send this var by the command 'localStorage', but it's not working for me. Ps:搜索了一下,我看到有一种通过命令'localStorage'发送这个var,但它对我不起作用。 :( :(

Can anybody help me?有谁能够帮助我?

Use localstorage a.html使用localstorage a.html

localStorage.setItem('id',1) 

b.html b.html

var id  = localStorage.getItem('id')

the other way is to put it in a js file and import it in both html另一种方法是将它放在一个js文件中并在两个html中导入它

Storing & Retrieving html data on the server在服务器上存储和检索 html 数据

Client Side JavaScript:
<script>
function saveId(v) {
  google.script.run.saveKeyValue({key:'id',value:v});
}
function getId() {
  google.script.run
  .withSuccessHandler(function(v){
     alert('The value is ' + v );
   })
  .getKeyValue('id');
}
</script>

Server Side Google Apps Script:

function saveKeyValue(obj) {
  PropertiesService.getScriptProperties().setProperty(obj.key,obj.value);
}

function getKeyValue(key) {
  return PropertiesService.getScriptProperties().getProperty(key);
}

You could also replace PropertiesService with CacheService.您还可以用 CacheService 替换 PropertiesService。

Client To Server Communications 客户端到服务器通信

Properties Service 物业服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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