Javascript 将谷歌表单转换为个人PDF并通过电子邮件发送

Javascript 将谷歌表单转换为个人PDF并通过电子邮件发送,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我有一个电子表格,它总是有一张名为“时间表”的表格,填写它的人可以根据需要创建任意多的附加注释表 我需要将除“时间表”表之外的所有表转换为单独的PDF并通过电子邮件发送到某个地址 我在创建一个脚本时遇到了问题,该脚本不能将它们滚动到单个PDF中。因此,它还包括PDF中的“时间表”表 我需要将每个笔记表转换为单个PDF,然后将所有PDF作为单独的附件通过电子邮件发送。填写笔记的人还可以将笔记重命名为他们想要的任何名称,因此我无法按名称获取该页 我有一些代码,结合了所有的工作表和重命名它,我用不同的

我有一个电子表格,它总是有一张名为“时间表”的表格,填写它的人可以根据需要创建任意多的附加注释表

我需要将除“时间表”表之外的所有表转换为单独的PDF并通过电子邮件发送到某个地址

我在创建一个脚本时遇到了问题,该脚本不能将它们滚动到单个PDF中。因此,它还包括PDF中的“时间表”表

我需要将每个笔记表转换为单个PDF,然后将所有PDF作为单独的附件通过电子邮件发送。填写笔记的人还可以将笔记重命名为他们想要的任何名称,因此我无法按名称获取该页

我有一些代码,结合了所有的工作表和重命名它,我用不同的目的,我会包括在下面,如果它是任何帮助

提前感谢您的帮助

function emailGoogleSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email address
  var email = "email@gmail.com"; 

  // Get the currently active spreadsheet URL (link)
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Custom
  var name = ss.getRange("Timesheet!J6:K6").getValue();
  var agency = ss.getRange("Timesheet!B4:C4").getValue();

  // Date
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();

  if (dd < 10) {
    dd = '0' + dd;
  }

  if (mm < 10) {
    mm = '0' + mm;
  }

  today = mm + '/' + dd + '/' + yyyy;

  // Subject of email message
  var subject = name + " has Submitted Their Timesheet and Notes"; 

  // Email Body can  be HTML too 
  var body = "This was submitted on " + today;

  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");

  blob.setName(name + "_" + agency + "_" + today + "_" + "timesheet_notes.pdf");

  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blob]     
    });
}
函数emailGoogleSpreadsheetAsPDF(){
//将电子表格的PDF发送到此电子邮件地址
var电子邮件=”email@gmail.com"; 
//获取当前活动的电子表格URL(链接)
var ss=SpreadsheetApp.getActiveSpreadsheet();
//习俗
var name=ss.getRange(“Timesheet!J6:K6”).getValue();
var agency=ss.getRange(“时间表!B4:C4”).getValue();
//日期
var today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;//一月是0!
var yyyy=today.getFullYear();
如果(dd<10){
dd='0'+dd;
}
如果(毫米<10){
毫米='0'+毫米;
}
今天=mm+'/'+dd+'/'+yyyy;
//电子邮件的主题
var subject=name+“已提交时间表和备注”;
//电子邮件正文也可以是HTML
var body=“该文件于”+今天提交;
var blob=DriveApp.getFileById(ss.getId()).getAs(“application/pdf”);
blob.setName(name+“”+机构+“”+今天+“”+“时间表”\u notes.pdf”);
//如果允许发送电子邮件,请发送带有PDF附件的电子邮件
如果(MailApp.getRemainingDailyQuota()>0)
GmailApp.sendmail(电子邮件、主题、正文、{
htmlBody:body,
附件:[blob]
});
}

通过电子邮件将电子表格作为单独的PDF发送

我不能仅仅通过创建blob并将它们放入数组来实现这一点。因此,我为每个页面创建了单独的PDF文件,然后在最后将其销毁。我还添加了和排除数组,这样您就可以从整个过程中排除某些文件,只发送您想要的工作表

当前版本包括在程序创建文件和发送电子邮件时跟踪程序进度的对话框提示。所以这和你的计划不完全一样

此外,这些行是我很难理解的程序,因为您有一个2单元数组,但您使用的是getValue()而不是getValue()

这是我的密码:

function savePDFFiles1() {
  var ss=SpreadsheetApp.getActive();
  var exclA=['Summary','Images','Globals','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
  var fA=[];
  var html="";
  var shts=ss.getSheets();
  var pdfFldr=DriveApp.getFolderById('FolderId');//folder where I stored the files temporarily
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    if(exclA.indexOf(name)==-1) {
      sh.showSheet();
      for(var j=0;j<shts.length;j++) {
        if(shts[j].getName()!=name) {
          shts[j].hideSheet();
        }
      }
      SpreadsheetApp.flush();//I dont know if this is required
      var file=pdfFldr.createFile(ss.getBlob().getAs('application/pdf').setName(Utilities.formatString('%s_%s.pdf',ss.getName(),name)));
      html+=Utilities.formatString('<br />File: %s Created',file.getName());
      var userInterface=HtmlService.createHtmlOutput(html);
      SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created')                                          
      fA.push(file);
    }
  }
  GmailApp.sendEmail('recipient email', 'Plot Reports', 'Plot Reports Attached', {attachments:fA})
  html+='<br />Email Sent';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');                                          
  for(var i=0;i<fA.length;i++ ) {
    fA[i].setTrashed(true);
  }
  html+='<br />Files Trashed and Process Complete';
  html+='<script>window.onload=function(){google.script.host.close();}</script>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');                                          
}
函数savePDFFiles1(){
var ss=SpreadsheetApp.getActive();
变量惊叹号=[‘摘要’、‘图像’、‘全局’、‘6’、‘7’、‘8’、‘9’、‘10’、‘11’、‘12’、‘13’、‘14’、‘15’、‘16’、‘17’、‘18’、‘19’、‘20’、‘21’];
var fA=[];
var html=“”;
var shts=ss.getSheets();
var pdfFldr=DriveApp.getFolderById('FolderId');//临时存储文件的文件夹

对于(var i=0;我必须隐藏所有你不想在pdf中使用的表单。这仍然会将它们合并到一个pdf中,我需要每个表单都是一个单独的pdf。我今天早上要做这件事,因为我想知道如何做哇,谢谢你关注这件事,我今晚回家后会对它进行测试。我的代码可能有一些问题在it方面,我不是开发人员,所以这是我在网上找到的一堆代码,并缝合在一起。再次感谢!到目前为止,它工作得很好,它将所有PDF作为个人发送给我。有两个小问题我不认为是很难解决的。第一个是,当它在所有工作表中循环时,它隐藏了其他工作表。最后,最后一个工作表我s显示,其余部分隐藏。我需要它仍然显示显示的所有工作表。第二个问题是“时间表”电子邮件中包含了用于数据和设置的工作表和两个隐藏的工作表。好的是,我需要排除的所有工作表都有固定的名称。那么,您是否可以添加一些额外的代码来按名称排除工作表?这很好!非常感谢。我最后做的一件事是拿走了我的一些原始co这是我添加的:
var ssa=SpreadsheetApp.getActiveSpreadsheet();var blob=DriveApp.getFileById(ssa.getId()).getAs(“application/PDF”);blob.setName(“文件名”);
然后我更改了
{附件:fA}
{附件:[fA,blob]}
但得到一个无效参数:attachments error。我如何在电子邮件中发送这两个附件?问题是,附件被描述为一个文件数组,而不是blob。我认为只需显示除排除项外的所有工作表并按下fA就可以了。哦,这是一种更好的方式。它一切都很好!非常感谢您的帮助,也感谢您优化了我的原始代码。我从中学到了很多。
function savePDFFiles1() {
  var ss=SpreadsheetApp.getActive();
  var exclA=['Summary','Images','Globals','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
  var fA=[];
  var html="";
  var shts=ss.getSheets();
  var pdfFldr=DriveApp.getFolderById('FolderId');//folder where I stored the files temporarily
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    if(exclA.indexOf(name)==-1) {
      sh.showSheet();
      for(var j=0;j<shts.length;j++) {
        if(shts[j].getName()!=name) {
          shts[j].hideSheet();
        }
      }
      SpreadsheetApp.flush();//I dont know if this is required
      var file=pdfFldr.createFile(ss.getBlob().getAs('application/pdf').setName(Utilities.formatString('%s_%s.pdf',ss.getName(),name)));
      html+=Utilities.formatString('<br />File: %s Created',file.getName());
      var userInterface=HtmlService.createHtmlOutput(html);
      SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created')                                          
      fA.push(file);
    }
  }
  GmailApp.sendEmail('recipient email', 'Plot Reports', 'Plot Reports Attached', {attachments:fA})
  html+='<br />Email Sent';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');                                          
  for(var i=0;i<fA.length;i++ ) {
    fA[i].setTrashed(true);
  }
  html+='<br />Files Trashed and Process Complete';
  html+='<script>window.onload=function(){google.script.host.close();}</script>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');                                          
}
function emailGoogleSpreadsheetAsPDF() {
  var email="email@gmail.com"; 
  var ss=SpreadsheetApp.getActive();
  var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
  var agency=ss.getRange("Timesheet!B4").getValue();//same here
  var fldr=DriveApp.getFolderById('folderId');
  var fA=[];
  var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
  var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name); 
  var body=Utilities.formatString('This was submitted on %s',today);
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    sh.showSheet();
    for(var j=0;j<shts.length;j++) {
      if(shts[j].getName()!=name) {
        shts[j].hideSheet();
      }
    }
    SpreadsheetApp.flush();//this may not be necessary...not sure
    var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
    fA.push(file);
  }
  GmailApp.sendEmail(email,subject,body, {attachments:fA});
  for(var i=0;i<fA.length;i++) {
    fA[i].setTrashed(true); 
  }
}
function emailGoogleSpreadsheetAsPDF() {
  var email="email@gmail.com";
  var exclA=['TimeSheet'];//and others
  var ss=SpreadsheetApp.getActive();
  var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
  var agency=ss.getRange("Timesheet!B4").getValue();//same here
  var fldr=DriveApp.getFolderById('folderId');
  var fA=[];
  var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
  var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name); 
  var body=Utilities.formatString('This was submitted on %s',today);
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    if(exclA.indexOf(name)==-1) {
      sh.showSheet();
      for(var j=0;j<shts.length;j++) {
        if(shts[j].getName()!=name) {
          shts[j].hideSheet();
        }
      }
      SpreadsheetApp.flush();//this may not be necessary...not sure
      var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
      fA.push(file);
    }
  }
  GmailApp.sendEmail(email,subject,body, {attachments:fA});
  for(var i=0;i<fA.length;i++) {
    fA[i].setTrashed(true); 
  }
  for(var i=0;i<shts.length;i++) {
    if(exclA.indexOf(shts[i].getName())==-1) {
      shts[i].showSheet();
    }
  }
}
function emailGoogleSpreadsheetAsPDF() {
  var email="email@gmail.com";
  var exclA=['TimeSheet'];//and others
  var ss=SpreadsheetApp.getActive();
  var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
  var agency=ss.getRange("Timesheet!B4").getValue();//same here
  var fldr=DriveApp.getFolderById('folderId');
  var fA=[];
  var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
  var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name); 
  var body=Utilities.formatString('This was submitted on %s',today);
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    if(exclA.indexOf(name)==-1) {
      sh.showSheet();
      for(var j=0;j<shts.length;j++) {
        if(shts[j].getName()!=name) {
          shts[j].hideSheet();
        }
      }
      SpreadsheetApp.flush();//this may not be necessary...not sure
      var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
      fA.push(file);
    }
  }
  for(var i=0;i<shts.length;i++) {
    if(exclA.indexOf(shts[i].getName())==-1) {
      shts[i].showSheet();
    }
  }
  SpreadsheetApp.flush();//this may not be necessary...not sure
  var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s.pdf',ss.getName()));
  fA.push(file)
  GmailApp.sendEmail(email,subject,body, {attachments:fA});
  for(var i=0;i<fA.length;i++) {
    fA[i].setTrashed(true); 
  }
}