将Google电子表格转换为PDF和电子邮件

时间:2014-04-29 15:56:27

标签: google-apps-script google-sheets

有人请帮助我,我一直在互联网上试图找到将当前电子表格转换为pdf并邮寄的脚本。我很确定这一天在我使用它的时候对我有用了它一直在说:

  

很抱歉,目前没有服务器可用。请稍等一下再试一次。 (第32行,文件" emailv1")

这是代码:

        function onOpen() {
          var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}];
          SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu);  
        }

        function exportSomeSheets() {
          var originalSpreadsheet = SpreadsheetApp.getActive();
          var message = "Please see attached";

          var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); 

          var period = originalSpreadsheet.getRange("B1:B1").getValues();


          var subject = projectname + " - Weekly Status Sheet - " + period;


          var contacts = originalSpreadsheet.getSheetByName("Contacts");
          var numRows = contacts.getLastRow();
          var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();


          var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
          var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
          var projectname = SpreadsheetApp.getActiveSpreadsheet();
          sheet = originalSpreadsheet.getActiveSheet();
          sheet.copyTo(newSpreadsheet);


          // Make zee PDF, currently called "Weekly status.pdf"
          // When I'm smart, filename will include a date and project name
          var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
          var attach = {fileName:'Weekly Status.pdf',content:pdf, mimeType:'application/pdf'};

          // Send the freshly constructed email 
          MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});

          // Delete the wasted sheet we created, so our Drive stays tidy.
          DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true);  
        }

1 个答案:

答案 0 :(得分:0)

也许这段代码可以帮到你。

function toPdf(id){
 SpreadsheetApp.flush();
// var ssDoc = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId());
  var ssDoc = DriveApp.getFileById(id);
 var pdf = DriveApp.createFile(ssDoc.getAs(MimeType.PDF));
  return pdf.getBlob().getBytes();
}