3

I'm trying to pass a variable (my name in cell A1) from my code.gs to my Index.html and email it.

Can someone please tell me what I'm doing wrong or point me in the right direction.

Code.gs

function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}

function sendEmail() {
var ss = SpreadsheetApp.openById('MY_SPREADSHEET_ID');
var sheet = ss.getSheetByName('Sheet1');

var myName = sheet.getRange(1,1).getValue();

var template = HtmlService.createTemplateFromFile('Index');
var body = template.evaluate().getContent();

// SEND THE EMAIL
MailApp.sendEmail({
  subject:"Test Email",
  to:"example@domain.com",
  htmlBody: body,
});

return myName;

Index.html

<!DOCTYPE html>

<html>  
  <body>
  <p><? var data = sendEmail(); ?><?= data ?></p>
  </body>
</html>
Justin Kaese
  • 321
  • 1
  • 8
  • I think the problem is recursion. In order to send the E-mail (function sendEmail) the template has to be evaluated, but in order to evaluate the template the function sendEmail has to be executed -> eternal loop. You can escape it by seperating the sendEmail function from the template into the doGet function. – Wim den Herder Sep 07 '15 at 07:16
  • @WimdenHerder sorry but I don't understand how i can separate them. Can you please give and example? Thanks for your help so far! – Justin Kaese Sep 07 '15 at 07:53
  • Have you already replaced `'MY_SPREADSHEET_ID'` with the actual spreadsheet ID ? It should look similar to this one: `1eTQYnI3jOKtyWF1V1DbuF8pdzpUwRNr9podKcoORD68`. – Rubén Sep 07 '15 at 18:17
  • Yes Rubén i just put that ID there for the purpose of this question – Justin Kaese Sep 07 '15 at 18:52
  • Good. If you will use Index.html to call the sendEmail function you should move the body of your email message to another place. I.E. you could build the body code in Code.gs or use a second HTML file. – Rubén Sep 07 '15 at 21:18
  • @Rubén I got it to work by doing what you said. Totally makes dollars! thanks :) – Justin Kaese Sep 08 '15 at 07:41
  • Great! Please, consider to post your working code as an answer with an explanation of the changes that you did. – Rubén Sep 08 '15 at 10:59

2 Answers2

2

Read this, pushing variables to templates

 function doGet() {
    return HtmlService.createHtmlOutputFromFile('Index');
    }

    function sendEmail() {
    var ss = SpreadsheetApp.openById('MY_SPREADSHEET_ID');
    var sheet = ss.getSheetByName('Sheet1');

    var myName = sheet.getRange(1,1).getValue();

    var template = HtmlService.createTemplateFromFile('Index');
    template.data = myName;
    var body = template.evaluate().getContent();

    // SEND THE EMAIL
    MailApp.sendEmail({
      subject:"Test Email",
      to:"example@domain.com",
      htmlBody: body,
});

Index.html

<!DOCTYPE html>

<html>  
  <body>
  <p><strong><?= data ?></strong></p>
  </body>
</html>
iJay
  • 4,205
  • 5
  • 35
  • 63
0

The question was still open when I found it and I had a similar problem, so here is my solution (without doGet()), based on the useful comments from Wim den Herder and Rubén (and the example in the documentation):

Code.gs

function sendEmail() {
var recipient = 'test@mail.de';
var subject = 'Test Email'; 
var message =     HtmlService.createTemplateFromFile('Index').evaluate().getContent();;


// SEND THE EMAIL 
GmailApp.sendEmail(
      recipient,         
      subject,                            
      message, {                        // body
      htmlBody: message                 // advanced options
    });

}

function getData(){
var ss = SpreadsheetApp.openById('MySpreadSheetId');
var sheet = ss.getSheetByName('Sheet1');
// Data is in cell A1
var myName = sheet.getRange(1,1).getValue();
return myName
}

Index.html

<!DOCTYPE html>

<html>  
  <body>
  <p><strong><? var data = getData() ?><?= data ?></strong></p>
  </body>
</html>
Albrecht
  • 135
  • 1
  • 8