-1

need help please to edit script in google sheet i would like to send automatically an email everyday with a table content. Can I ? i found this code

    function sendmail1() { 
  
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange(1, 1, 22, 7).getValues();
  var email = "xxx@xxx.fr";
  var subject = "test";
  var body = "body";
  
  data.forEach(function(column){ 
    
    body += column[0] + " " + column[1] + " " + column[2] + " " + column[3] + " " + column[4] + " " + column[5] + " " + column[6] + "<br>";  
  });
  
  if (MailApp.getRemainingDailyQuota() > 0)
   GmailApp.sendEmail(email, subject, body, {
      htmlBody: body
    });
} 

and i set the trigger this work but i receive the data without the table, no layout, nothing can i have a table layout and data format ?

(hope my english is not too bad? ;-) )

thank you for your help

update

function sendmail1() { 
  
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var gid = sheet.getSheetId();
 var pdfOpts = '&size=A3&fzr=true&portrait=false&fitw=true&gridlines=false&printtitle=true&sheetnames=true&pagenumbers=true&attachment=false&gid='+gid;
 var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts
 var options = {
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
      }
    }
  var blobresponse = UrlFetchApp.fetch(url, options);
  var blob=blobresponse.getBlob().setName(ss.getName() + " - " + CandidateName+".pdf" );
  var emailAddress=Session.getActiveUser().getEmail();
  var mess="Voulez-vous envoyer votre rapport  à l'adresse : " + emailAddress;
  var ans= Browser.msgBox("Courriel", mess, Browser.Buttons.YES_NO);
  if (ans===Browser.Buttons.NO){return;}
  var mess="Votre rapport a été envoyé à l'adresse : " + emailAddress;
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName("Recherche");
  var CandidateName=ss.getRangeByName("Nom.Candidat").getValue();
  var emailSubject="Vérifications pré-emploi complétées" +" - "+ CandidateName;
  var emailMessage="Bonjour," + "\n\n" + "J’ai le plaisir de vous informer que les vérifications sont complétées pour le candidat indiqué au tableau de résultats pré-emploi suivant:" + "\n\n" + "Bonne journée !";
  var shts=ss.getSheets();
  var hdnA=[];
  shts.forEach(function(sht){if(sht.getName()!="Recherche") {sht.hideSheet();hdnA.push(sht.getName());}})
  MailApp.sendEmail(emailAddress, emailSubject, emailMessage,{attachments:[blob]});
  hdnA.forEach(function(name){ss.getSheetByName(name).showSheet();})
  Browser.msgBox("Courriel", mess, Browser.Buttons.OK); 
}

update2

 /* Envoyer la feuille de calcul par courriel au format PDF */

function emailFeuilleDeCalculVersPDF() {
  
  // Email réceptionant le PDF de cette feuille de calcul
  var email = "xxx@xxx"; 
  
  // Obtenir l'URL de la feuille de calcul actuellement active (lien)
  var feuille = SpreadsheetApp.getActiveSpreadsheet();
  
  // Sujet du message
  var sujet = "PDF généré depuis la feuille de calcul " + feuille.getName(); 
  
  // Corps du mail
  var corpsDuMessage = "<p>Bonjour,</p>Veuillez trouver en pièce jointe le PDF de votre feuille de calcul.<p>Bonne réception,</p>";
  
  var contenant = DriveApp.getFileById(feuille.getId()).getAs("application/pdf");
  
  contenant.setName(feuille.getName() + ".pdf");
  
  // Si vous n'avez pas dépassé le quota, envoi du mail avec la pièce jointe en PDF.
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, sujet, corpsDuMessage, {
      htmlBody: corpsDuMessage,
      attachments:[contenant]     
    });  
}

1 Answers1

1

You need to either convert the sheet contents to pdf or build a custom HTML table

To build a simple html table manually you, you can do something like:

  var body = "body";
  body +="<table>";  
  data.forEach(function(row){ 
    body +="<tr>";
    row.forEach(function(column){
      body += "<td>" + column + "</td>"
      // body += row[0] + " " + row[1] + " " + row[2] + " " + row[3] + " " + row[4] + " " + row[5] + " " + row[6] ; 
    })
    body +="</tr>";
    
  });
  body +="</table>";
  
  if (MailApp.getRemainingDailyQuota() > 0){
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body
    });
  }
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • nice thanks and how can i keep number's format ? (as pourcent ?) and cell's format ? (background or text color, etc ?) – Maison Guéret 1880 Aug 13 '20 at 07:06
  • and can i have a graphic as well ? – Maison Guéret 1880 Aug 13 '20 at 07:30
  • If you make a HTML table - you need to manually recreate the style of the table in Googlesheets. You can retrieve this style with Apps Script methods, e.g. [getBackgrounds()](https://developers.google.com/apps-script/reference/spreadsheet/range#getbackgrounds) and then implement in into the HTML table with [HTML style methods](https://www.w3schools.com/css/css_table.asp). – ziganotschka Aug 13 '20 at 07:35
  • However, this is laborous as you can see. Maybe in your case it is better to convert the table to [xls](https://stackoverflow.com/questions/62324006/google-apps-script-send-google-sheet-as-email-attachment/62325901#62325901) or [pdf](https://stackoverflow.com/questions/58322709/specify-pdf-printing-options-within-gas/58325297#58325297) for sending, or jsut share the link to the Google Sheet? – ziganotschka Aug 13 '20 at 07:35
  • ou are right it's laborious. i didn't found how make percent. so how can i convert in pdf ? it will be in attachment or in the body ? and like this i'll have my graphics – Maison Guéret 1880 Aug 13 '20 at 08:52
  • You can do it with [UrlFetchApp.fetch()](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app) as done [here](https://stackoverflow.com/questions/58322709/specify-pdf-printing-options-within-gas/58325297#58325297) – ziganotschka Aug 13 '20 at 09:00
  • sorry but i don't understand how it work. before i had an automatic sending by email of my report. here it ask for athourization and after stay in running function ... and nothing ... and i dont khnow where i set the mail adress of receiver could you please adapt my old script to keep the automatic sending of a pdf or xls which stay at format in spreadsheet ? – Maison Guéret 1880 Aug 13 '20 at 14:52
  • finally i got type error cannot read porperty 'getvalue' of null lgne 22 and it's not automatic :-( – Maison Guéret 1880 Aug 13 '20 at 14:59
  • Please update your question with your new code including the line that gives you the error – ziganotschka Aug 13 '20 at 15:00
  • i did. i copied pasted the code you show me. but i want keep the autmatic sending as you show me before. can we mix 2 scripts ? – Maison Guéret 1880 Aug 13 '20 at 15:04
  • In the specific script fromt he other post a user wants to convert to pdf data from a named range called `"Nom.Candidat"`. In your case you do not have such a range - this gives you an error. Also - I assume that you do not have a sheet named `"Recherche""`. The idea is that you out some effort into understanding the script and adapting it to your case. – ziganotschka Aug 13 '20 at 15:41
  • ok i'm lost ... :-( i'll find a plugin thank your for your help – Maison Guéret 1880 Aug 13 '20 at 15:49
  • i found the script in update2 in my question it work perfectly and very easy ! – Maison Guéret 1880 Aug 13 '20 at 15:58