【问题标题】:how to edit script to send table google sheet by mail?如何编辑脚本以通过邮件发送表格谷歌表?
【发布时间】:2020-08-12 11:01:19
【问题描述】:

需要帮助,请在 google sheet 中编辑脚本 我想每天自动发送一封包含表格内容的电子邮件。 我可以吗 ? 我找到了这段代码

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

然后我设置了触发器 这项工作,但我收到没有表格的数据,没有布局,什么都没有 我可以有一个表格布局和数据格式吗?

(希望我的英语不会太差?;-))

感谢您的帮助

更新

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

更新2

 /* 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]     
    });  
}

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    您需要将工作表内容转换为 pdf 或构建自定义 HTML table

    要手动构建一个简单的 html 表,您可以执行以下操作:

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

    【讨论】:

    • 非常感谢,我怎样才能保持数字的格式? (如pourcent?)和单元格的格式? (背景或文字颜色等?)
    • 我也可以有一个图形吗?
    • 如果您制作 HTML 表格 - 您需要在 Googlesheets 中手动重新创建表格样式。您可以使用 Apps 脚本方法检索此样式,例如getBackgrounds() 然后用HTML style methods 实现到 HTML 表中。
    • 但是,如您所见,这很费力。也许在您的情况下,最好将表格转换为 xlspdf 以进行发送,或者直接分享 Google 表格的链接?
    • 你说得对,这很费力。我没有找到百分比。那么我怎样才能转换成pdf呢?它会在附件中还是在身体中?像这样我会有我的图形
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多