[英]Pass value from HTML to Google Script on Google Apps Script?
I need to pass a data value from the HTML to the Google Script on a GAS project.我需要将 HTML 中的数据值传递给 GAS 项目的 Google 脚本。 The parts with comments of the code is the value I need to pass.
带有代码注释的部分是我需要传递的值。
gs file .gs 文件
function doGet(e) {
if (e.parameter.prefix){
var result = data; // <-- GET VALUE "data" FROM THE HTML "forms.html"
var content = e.parameters.prefix + '(' +JSON.stringify(result) + ')';
return ContentService.createTextOutput(content)
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
var html = HtmlService.createHtmlOutputFromFile('Index');
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
Index.html in GAS project GAS项目中的索引.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1 id='title' style="color: #e31414">SCRIPT</h1>
<h1 id='done' style="display: none"> DATA PASSED</h1>
<button id="btn" onclick="passData()">Run "passData()"</button>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
function passData() {
$('#done').show();
var data = 'value'; //<-- PASS THIS DATA TO GOOGLE SCRIPT (The .gs file)
}
</script>
Follow these steps:按着这些次序:
var data;
var data;
function myfunction(x) {
data = x;
}
passData()
function, insert this line in the bottom of the function: google.script.run.myfunction(data);
passData()
function 中,将此行插入 function 的底部: google.script.run.myfunction(data);
Finally, you will be able to use the result
variable as the data in doGet()
function.最后,您将能够使用
result
变量作为doGet()
function 中的数据。
-- your code should look like this -- --你的代码应该是这样的--
gs file .gs 文件
var data;
function myfunction(x) {
data = x;
}
function doGet(e) {
if (e.parameter.prefix){
var result = data; // <-- GET VALUE "data" FROM THE HTML "forms.html"
var content = e.parameters.prefix + '(' +JSON.stringify(result) + ')';
return ContentService.createTextOutput(content)
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
var html = HtmlService.createHtmlOutputFromFile('Index');
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
Index.html in GAS project GAS项目中的索引.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1 id='title' style="color: #e31414">SCRIPT</h1>
<h1 id='done' style="display: none"> DATA PASSED</h1>
<button id="btn" onclick="passData()">Run "passData()"</button>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
function passData() {
$('#done').show();
var data = 'value'; //<-- PASS THIS DATA TO GOOGLE SCRIPT (The .gs file)
google.script.run.myfunction(data);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.