0

I have a web app which take some inputs and I need to write those inputs in a sheet and then send an email with those inputs.

My code is below, I removed some lines in the code because they are not relevant. Assume pnArray and qArray are two arrays with values (strings and numbers).

The problem statement is how to create a HTML table in the loop below and then send that table by email.

function submitInfo() {
    
  var table = "<table><tbody><tr><td>Part Number</td><td>Packs</td></tr>";
  var pnArray = ["string1","string2"];
  var qArray = [1,2]; 
    
    for(var i =0;i<pnArray.length;i++){
    
    var pn = pnArray[i];
    var q = qArray[i];
      
    table += Utilities.formatString('<tr><td>%s</td>', pn);
    table += Utilities.formatString('<td>%s</td></tr>', q);
     
      
    }
    table+="</tbody></table>";
    GmailApp.sendEmail("[email protected]", "test", null, {htmlBody:table});
}
2
  • I need a minimal reproducible example with no undefined variables. Commented Jul 1, 2020 at 21:29
  • Hi, I edited as above, is that ok? Commented Jul 1, 2020 at 21:41

1 Answer 1

1
function submitInfo() {
  var html = "<table><tr><td>Part Number</td><td>Packs</td></tr>";
  var pnArray = ["string1","string2"];
  var qArray = [1,2]; 
  pnArray.forEach(function(pn,i){html+=Utilities.formatString('<tr><td>%s</td><td>%s</td></tr>'pn,qArray[i]);});
    table+="</table>";
    GmailApp.sendEmail("[email protected]","test",null,{htmlBody:html});
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer, I kept my loop format as I am having other code in my script, it does send the email, but the format of the table is off. Even with your code, there are no borders. How can I change the style?
try using an inline style attribute in the th and td
not sure how.. I added <table style="border-color: #d8d3db;"> but still no showing the border. I used similar syntax to center the text and it works. So the issue seems only for the borders?
I don't really send any html email. But my guess is may style='border: 1px solid #d8d3db'

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.