简体   繁体   English

似乎无法在Google Web App Script中将多个参数从JS传递到HTML

[英]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.

相关问题 Google App : 同一个 App Project 中有多个 HTML 或脚本文件并添加登录? - Google App : More than one HTML or script file in the same App Project and add Login? 似乎无法通过jquery将多个变量传递给mysql - Can not seem to pass more than one variable with jquery to mysql 如何将值从HTML表单提交传递给Google表格,然后再传递回Google Apps Script Web App中的HTML - How do I pass a value from an HTML form submission to a Google Sheet and back to HTML in a Google Apps Script Web App 使用Google Web应用程序-我可以在同一Web应用程序中将变量从一个HTML页面传递到另一个HTML页面 - Using a Google Web App - Can I pass a variable from one HTML page to another within the same web app 在HTML页面中添加多个脚本 - adding more than one script into an html page 如何推迟多个js脚本 - How to defer more than one js script 不能多次更改html元素的不透明度 - cant change html element's opacity more than one time html5 / javascript是否有任何功能可防止在多个浏览器实例中打开同一Web应用程序? - Is there anything in the works with html5/javascript to prevent opening the same web-app in more than one browser instance? 将 html 模板中的 js 变量传递给 google 脚本 - Pass js variable in html template to google script 将文件上传的多个参数发布到 web api - Post more than one parameter for file upload to the web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM