[英]Cant seem to pass more than one parameter from JS to HTML in Google Web App Script
I am learning how to create a very basic Google Web App ,trying to grab 2 variables from a function (a fisrt name and surname from a google sheet) and return them to the HTML code to display on my page. 我正在学习如何创建一个非常基本的Google Web App,尝试从函数中获取2个变量(来自google工作表的fisrt名称和姓氏)并将它们返回到HTML代码以显示在我的页面上。 It works grabbing just one variable but when I try returning 2 it all gets messed up. 它只能抓取一个变量,但是当我尝试返回2时,它们都搞砸了。 Any help would be very much appreciated. 任何帮助将非常感谢。
My JS code: 我的JS代码:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
};
function grabUser(){
var userEmail = Session.getActiveUser().getEmail();
var SpreadsheetId = '1xxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxx';
var SheetName = 'MASTER';
var EmailCol = 7; //keep not that column A=0, B=1...
var FNameCol = 1;
var SNameCol = 0;
// Log the email address of the person running the script.
Logger.log(userEmail);
var ss = SpreadsheetApp.openById(SpreadsheetId);
var sheet = ss.getSheetByName(SheetName);
var data = sheet.getDataRange().getValues(); // read all data in the sheet
for(n=0;n<data.length;++n){ // iterate row by row and examine data in col
if(data[n][EmailCol].toString()==userEmail){ var FName = data[n][FNameCol];
var SName =data[n][SNameCol]};// return the values of these cells
}
Logger.log(FName);
Logger.log(SName);
return (FName, SName);
};
and then my HTML: 然后是我的HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function onSuccess(FName,SName) {
var div = document.getElementById('output');
div.innerHTML = 'Hello ' + FName +
' Your surname is '+SName;
}
google.script.run.withSuccessHandler(onSuccess).grabUser();
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
Two parameters are not allowed in the argument list of the function definition from the server back to the Web App. 从服务器返回到Web App的函数定义的参数列表中不允许有两个参数。
For the documentation on this, see: 有关此文档,请参阅:
Apps Script Documentation - Parameters and Return Values Apps脚本文档 - 参数和返回值
Quote: 引用:
Legal parameters and return values are JavaScript primitives like a Number, Boolean, String, or null, as well as JavaScript objects and arrays that are composed of primitives, objects and arrays. 合法参数和返回值是JavaScript原语,如Number,Boolean,String或null,以及由基元,对象和数组组成的JavaScript对象和数组。
To pass multiple pieces of information back, you'd probably want to use an array in your case: 要传回多条信息,您可能希望在您的情况下使用数组:
return [FName, SName];
For more data, a good option is to put all the data into an object, and then convert the object to a string: 对于更多数据,一个好的选择是将所有数据放入一个对象,然后将该对象转换为字符串:
var myObject = {'one':'value','two':'value'};
myObject = JSON.stringify(myObject);
return myObject;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.