user1682683
user1682683

Reputation: 293

Google Apps Script HTML Service: Passing variables

I have a simple function that retrieves the session user email and returns it. I would like to run this function and return this value when I click a button in my html service template. Unfortunately it is always undefined. Below is a short snipped of what I am trying to do.

//code.gs
function getUser(){

var user = Session.getActiveUser().getEmail();  
Logger.log('User was called')
return user;

}

//Page.html
 <p>Your username: <?= user ?> </p> \\this works ok. 

 <button type="button" onclick="subMe()"id="submit" >Submit</button>

<script>
function subMe(){
var user = google.script.run.getUser(); //Logger works but no return value!

}

 function setUser(activeUser){
   alert(activeUser);
   return activeUser;
 }

 google.script.run.withSuccessHandler(setUser).getUser(); //alert works OK, but no return value

So in the end I want to call this function and return the session user and have that stored in a variable so I can use it later. Any tips appreciated!

Regards

Shawn

Upvotes: 0

Views: 4778

Answers (1)

Harold
Harold

Reputation: 3337

This is not possible as the call to google.script.run are asynchronous. See this question for workaround: how to use google.script.run as if it was a function

Upvotes: 2

Related Questions