在单个PDF中导出多个工作表

时间:2017-11-14 15:45:33

标签: google-apps-script

只是想知道我如何能够导出PDF文件以包含多个工作表,但不能全部导出工作簿。我只能将单张纸作为单独的PDF附件发送。对此的任何见解都会很棒。

  var url = ss.getUrl(); //gets url 
  url = url.replace(/edit/,''); 

  var url_ext = 'export?exportFormat=pdf&format=pdf' + 
  '&size=letter' + 
  '&portrait=false' + 
  '&fitw=true' + 
  '&sheetnames=false&printtitle=false' +  
  '&pagenumbers=true&gridlines=false' + 
  '&fzr=true' +  
  '&gid=GID'

  var token = ScriptApp.getOAuthToken(); 
  var response = UrlFetchApp.fetch(url + url_ext, { 
      headers: { 
        'Authorization': 'Bearer ' +  token 
      } 
    }); 
      var blob = response.getBlob().setName(sheet.getName() + '.pdf'); 

1 个答案:

答案 0 :(得分:0)

安德鲁·罗伯茨在此blog post中已经写了一篇关于此的指南,该帖子的源代码如下:

function convertSpreadsheetToPdf(email, spreadsheetId, sheetName, pdfName) {

  var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
  spreadsheetId = spreadsheetId ? spreadsheetId : spreadsheet.getId()  
  var sheetId = sheetName ? spreadsheet.getSheetByName(sheetName).getSheetId() : null;  
  var pdfName = pdfName ? pdfName : spreadsheet.getName();
  var parents = DriveApp.getFileById(spreadsheetId).getParents();
  var folder = parents.hasNext() ? parents.next() : DriveApp.getRootFolder();
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');

  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf

      // Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
      + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 
      // following parameters are optional...
      + '&size=letter'      // paper size
      + '&portrait=true'    // orientation, false for landscape
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
      + '&gridlines=false'  // hide gridlines
      + '&fzr=false';       // do not repeat row headers (frozen rows) on each page

  var options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }

  var response = UrlFetchApp.fetch(url_base + url_ext, options);
  var blob = response.getBlob().setName(pdfName + '.pdf');
  folder.createFile(blob);

  if (email) {

    var mailOptions = {
      attachments:blob
    }

    MailApp.sendEmail(
      email, 
      "Here is a file named " + pdfName, 
      "Please let me know if you have any questions or comments.", 
      mailOptions);
  }

} // convertSpreadsheetToPdf()

function test() {

  var TEST_EMAIL = 'your email address'

  // Create a PDF containing all the tabs in the active spreadsheet, name it
  // after the spreadsheet, and email it
  convertSpreadsheetToPdf(TEST_EMAIL)

  // Create a PDF containing all the tabs in the spreadsheet specified, name it
  // after the spreadsheet, and email it
  convertSpreadsheetToPdf(TEST_EMAIL, '1r9INcnsyvSQmeduJWVYAvznOOYei9jeAjsy0acA3G1k')

  // Create a PDF just containing the tab 'Sheet2' in the active spreadsheet, specify a name, and email it
  convertSpreadsheetToPdf(TEST_EMAIL, null, 'Sheet2', 'PDF 3')
}