0

THIS SCRIPT IS INTENDED TO ITERATE DOWN A LIST OF SPREADSHEET DATA AND SEND A HTML FORMATTED EMAIL WITH THAT DATA TO MY EMAIL ADDRESS. CODE.GS IS TO PASS THE VARIABLES TO INDEX.HTML SO THEY CAN BE FORMATTED NICELY AND SENT AS A HTML TEMPLATE. DATE: 09/09/2015 JFKAESE @ GMAIL . COM

CODE.GS

// THIS FUNCTION TAKES THE ITERATOR VARIABLE TO GO DOWN SHEET DATA
function emailBody(i){

  //GET SPREADSHEET BY ID AND SHEET NAME
  var ss = SpreadsheetApp.openById('1Wc0j3twn1i-ug-lCsWOxyk-8u9t21Vr_xJH9wVu4KTk');
  var sheet = ss.getSheetByName('Sheet1');

  // GET DATA AND RETURN IT FOR THE HTML BODY
  var item1 = sheet.getRange(i,1).getValue();
  var item2 = sheet.getRange(i,2).getValue();
  return [item1, item2];

} 

// THIS FUNCTION LOOPS < 5 FOR TESTING PURPOSES BUT SHOULD GO
// UNTIL THE LAST ROW OF DATA

function looper() {
  for (var i = 1; i < 5; i++){

    // LOG THE OUTCOME -- WORKING PERFECT.
    // I NEED TO SEND THE DATA TO THE INDEX.HTML FOR FORMATTING
    Logger.log(emailBody(i));
  }
}

// THIS FUNCTION IS TO SEND THE EMAILS TO MY EMAIL ADDRESS
function sendEmails() {
  var template = HtmlService.createTemplateFromFile('Index');
  var body = template.evaluate().getContent();

  // SEND THE EMAIL
  MailApp.sendEmail({
    subject:"Test Email",
    to:"[email protected]",
    htmlBody: body,
  });
}

INDEX.HTML

<!DOCTYPE html>

<html>

  <body style="background-color:#F5F5F5">


    <p><? var data = emailBody(); ?></p>
    <p><? var item1 = data[0]; ?></p>
    <p><? var item2 = data[1]; ?></p>
    <p><?= item1 ?>
    <p><?= item2 ?>

  </body>

</html>
3
  • Did you check the answer??? Commented Sep 8, 2015 at 13:32
  • @psylogic ive posted my code. hopefully you can make sense of it . Commented Sep 9, 2015 at 7:25
  • @psylogic the idea is to send one html formatted email per line of spreadsheet data Commented Sep 9, 2015 at 7:26

2 Answers 2

1

Change the function definition a little bit and call it inside a loop. And this is how you'll have two values returned for every iteration.

 function emailBody(i){
          var ss = SpreadsheetApp.openById('A_SHEET_ID_HERE');
          var sheet = ss.getSheetByName('Sheet1');


            var item1 = sheet.getRange(i,1).getValue();
            var item2 = sheet.getRange(i,2).getValue();
            return [item1, item2];


        } 
 for (var i = 1; i < 5; i++){
    console.log( emailBody(i));
    }

For passing the value you can do it like this, how do you want to get the values is upto you:

HTML

<p id="item1"></p>
<p id="item2"></p>

JAVASCRIPT

document.getElementById("item1").innerHTML=item1;
document.getElementById("item2").innerHTML=item2;
Sign up to request clarification or add additional context in comments.

3 Comments

This code is great and it does exactly want you say it does. My problem now is that I cannot figure out how to pass the returned values to my .html file.
Thanks for your help. I can't work it out. I might be a little over my head with this one. ill work on it and post if i can make it happen. Thanks again!
you can tell me exactly what you want to do and i'll tell you how to do it!
0

Store the items you want returned into a variable so that the for loop can finish executing and then execute the return statement with variable being passed at the end of the for loop?

Comments

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.