Log in and send data with GAS

1

I am working with a code that should validate a user and open a form with filtered data for that user. To open the form after validating, use a dynamic method that was published here and works well for me:

var rutaWeb = ScriptApp.getService().getUrl();
function doGet(e) {
 var page = e.parameter.p || "myPrueba"; 
 return HtmlService.createTemplateFromFile(page).evaluate()
 .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

in the index:

function abrirUrl(url) {
        var a = document.createElement("a");
        a.target = "_blank";
        a.href = url;
        a.click();            
    }

    var url = "<?!= rutaWeb + '?p=Inicio' ?>";

function abrirInicio(){
        var tname = document.getElementById("nomcon").value;
        abrirUrl(url);

        //enviar tname???
    }

I've tried several options here link  but they do not work for me.

Am I doing everything wrong or am I missing?

    
asked by Karen Lewis 15.03.2018 в 06:05
source

2 answers

2

One of the things you missed was passing the global variable to the template.

Example:

Code.gs

// Variables globales
var mensaje = 'Hola mundo';

// Devuelve una página web
function doGet() {
  var template = HtmlService.createTemplateFromFile('index');
  template.mensaje = mensaje; // Esta es la línea de código faltante

  var html = template.evaluate();
  return html;
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <?!= mensaje ?>
  </body>
</html>

Related questions

answered by 15.03.2018 в 06:57
2

To send the validated user information, I had better results using CacheService.getUserCache () declaring it global in the .gs:

var rutaWeb = ScriptApp.getService().getUrl();
var cache = CacheService.getUserCache();

function doGet(e) {
 var page = e.parameter.p || "myPrueba"; 
 var template = HtmlService.createTemplateFromFile(page);

 var user = cache.get("USER");
 template.user = user;

 var html = template.evaluate()
            .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
 return html;
}

How do I need the name of the user who started the session to recover the data when validating it:

index.html

function abrirInicio(){
 var user = document.getElementById("nombre").value;
 google.script.run.withSuccessHandler().getUser(document.forms[0]);
 abrirUrl(url);        
} 

Codigo.gs

function getUser(e){
 cache.put("USER", e.nomcon); 
}

Welcome.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_blank">
  </head>
  <body>
    <p>Bienvenido <?!= user ?></p>
  </body>
</html>
    
answered by 17.03.2018 в 03:14