1

I'm triyng to pass a simple variable (profesorgg) to an HTML template in google Apps Script in order to evaluate the HTML for each teacher in my school and send to them a personalized email. This is the code:

Code.gs

function Recordatorio() {

//COGIENDO UN PROFESOR DESDE GROUPS

var page = AdminDirectory.Users.list({
  domain: 'iesovaldemedel.es',
  orderBy: 'familyName',
  maxResults: 400});
var has = GroupsApp.getGroupByEmail("[email protected]").getUsers();


var users = page.users;
if (users) {
  for (var i = 0; i < users.length; i++) {

    var email = users[i].primaryEmail;
    var nombre = users[i].name.givenName;
    var apellidos = users[i].name.familyName;
for (var h in has){

    if (has[h] == email){

var profesorgg = apellidos + ", "+nombre;

var subject ="Resumen de faltas para " +profesorgg + "." ;
var body = "Se adjunta el resumen de faltas del mes que termina"; 
var html = HtmlService.createTemplateFromFile("recordatoriohtml");
html.data = profesorgg;
var htmlbody =html.evaluate().getContent();

MailApp.sendEmail("[email protected]", subject, body, {htmlBody: 
htmlbody});

}}}}}

HTML FILE: recordatoriohtml

<html>
<head>

</head>
<body>

<h1 align="center">IESO Valdemedel</h1>
<div class = "wrapper3">
<image align="center" src="image>
</image>
</div>

<? var today = new Date;
var mes = today.getMonth();
var ano = today.getYear();
var date = Utilities.formatDate(today, "GMT+1", "dd-MM-yyyy");
var data = SpreadsheetApp.openById("myspreadsheet")
.getSheetByName("DATOS").getDataRange().getValues();

I`m triyng to call profesorgg HERE (HMTL file continuation)

var profesorg = profesorgg;
var faltamas=0;
var faltas =[];
//BUSCANDO A ESE PROFESOR EN LAS FALTAS

for ( var o = 1 ; o<data.length; o++){
var timestamp = data[o][0];

var profesor = data[o][1];
var diadefalta = data[o][2];
var todoeldia = data[o][4];
var motivo = data[o][3];


if(diadefalta != ""){
var diadefalta2 =  Utilities.formatDate(diadefalta, "GMT+2", "dd-MM-yyyy");
var mesfalta = diadefalta.getMonth();
var anofalta = diadefalta.getYear();}
else{var diadefalta = "-"}


if (ano == anofalta && mes == mesfalta && profesor == profesorg){
faltamas++
faltas.push([diadefalta2,profesor,todoeldia,motivo]);}}

if(faltamas >0){?>

<hr width=100%>
<h1 align = "center">Resumen Futuro</h1>
<hr width=100%>

<h2> Faltas para mañana </h2>
<?if(faltas !=""){?>
<div>
<table style="width:100%">
<tr>
<th id="t01">Fecha</th>
<th id="t02">Todo el día</th> 
<th id="t03">Motivo</th>

</tr>
<?for (var c=0; c<faltas.length; c++){?>
<tr>
<td id="td01"><?=faltas[c]?></td>

<td id="td02"><?=faltas[c][0]?></td> 
<td id="td03"><?=faltas[c][2]?></td>
<td id="td04"><?=faltas[c][3]?></td>

</tr>
<?}}}?>
</table>
<br>
<hr width=100%>

<?if (faltas ==""){?>
No tienes faltas este mes
<?}?>
<br>

</div>

<hr width=100%>

<h3>Atentamente, IESO Valdemedel</h3>
<hr width=100%>
</body>
<footer>

<b><i><small>Powered by</small></i></b> <br> 
<a href="http://www.teachinginsights.es">
<img src="image>
</a>

<hr width=100%>
</footer>
</html>

¿It is possible? Thanks a lot for your help

Mario Moreno

1 Answer 1

2

If I read your code right, you are passing the variable "profesorgg" like this:

 html.data = profesorgg;

Which is the right way to go, but note that you are actually declaring a variable "data" to your html and assigning the value of profesorgg to it. This means that in your html

<? data ?> 

would return the value of profesorgg execpt that I think you are overriding it in the line

 var data = SpreadsheetApp.openById("myspreadsheet")
.getSheetByName("DATOS").getDataRange().getValues();

anyway, for the next line to work in your html - file:

var profesorg = profesorgg;

you would need to do this in your .gs file:

 html.profesorg = profesorgg;
Sign up to request clarification or add additional context in comments.

2 Comments

It works!!. Thanks a lot. I was going crazy with this. Thanks again.
I am most glad to help! I will appreciate if you mark the answer as accepted, but I am nevertheless pleased that I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.