Capt_Sensible Capt_Sensible - 1 year ago 233
Javascript Question

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. It works grabbing just one variable but when I try returning 2 it all gets messed up. Any help would be very much appreciated.

My JS code:

function doGet() {

return HtmlService.createHtmlOutputFromFile('Index')

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.

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

return (FName, SName);


and then my HTML:

<!DOCTYPE html>
<base target="_top">
function onSuccess(FName,SName) {
var div = document.getElementById('output');
div.innerHTML = 'Hello ' + FName +
' Your surname is '+SName;
<div id="output"></div>

Answer Source

Two parameters are not allowed in the argument list of the function definition from the server back to the Web App.

For the documentation on this, see:

Apps Script Documentation - Parameters and Return Values


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.

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download