Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

how to edit script to send table google sheet by mail?

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 = "[email protected]";
  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," + "

" + "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:" + "

" + "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]     
    });  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    });
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...