Search code examples
google-apps-scriptrangesendmail

Send range via email with google script


I want to send range by email

For example:

https://docs.google.com/spreadsheets/d/1GH5r0sY_Z-15M479xG12yCUZaV0NRU8jpkWAxPNBuwU/edit?usp=sharing

I tried this:

function sendInvoiceList(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invoice").getRange("A1:B10").getValues();
  var message = sheet;
  var email_subj = "Invoice " + Utilities.formatDate(new Date(), "GMT+1:00", "dd.MM.yyyy");
   MailApp.sendEmail({
     to: "[email protected]",
     subject: email_subj,
     htmlBody: "Please send us this list:<br><br><br>" + message
 });
}

The system send this: textA1,textA2,textA3,textA4,textA5,textA6,textA7,textA8,textA9,textA10

I need value in range A1:B10 but the system only A1 separated by comma I need in column format.

How do it? Thanks for support Angelo


Solution

  • Your setting message equal to a two dimensional array you need to rethink that. I think this maybe closer to what you want. I tested it with some data of my own but don't know what you have. Share an image of your spreadsheet if you need more assistance.

    function sendInvoiceList(){
      var ss=SpreadsheetApp.getActive()
      var sh=ss.getSheetByName("Invoice");
      var rg=sh.getRange("A1:B10")
      var vA=rg.getValues();
      var html='<style>td,th{border:1px solid black;}</style><table>';
      vA.forEach(function(r,i){
        html+='<tr>';
        r.forEach(function(c,j){
          if(i=0) {
            html+=Utilities.formatString('<th>%s</th>',c)
          }else{
            html+=Utilities.formatString('<td>%s</td>',c)
          }
        });
        html+='</tr>';
      });
      html+='</table>';
      var email_subj = "Invoice " + Utilities.formatDate(new Date(), "GMT+1:00", "dd.MM.yyyy");
      MailApp.sendEmail({to: "[email protected]",subject: email_subj,htmlBody: "Please send us this list:<br><br><br>" + html});
     //SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'Display');//Just a dialog for testing the html
    }