PDF边距 - Google Script

时间:2017-07-31 17:31:11

标签: pdf google-sheets pdf-generation google-sheets-api

我的基本脚本运行正常,它完全符合我的要求。谷歌表将其转换为PDF并通过电子邮件发送给我PDF。

我的问题是如何调整PDF上的边距,我需要将PDF设置为适合页面。我无法调整工作表的大小,因为它会抛弃间距。

    /* Email Google Spreadsheet as PDF */
function PDF() {

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

  // Get the currently active spreadsheet URL (link)
  var ss = SpreadsheetApp.openByUrl(
     'https://docs.google.com');

  // Subject of email message
  var subject = "PAR - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue(); 

  // Email Body can  be HTML too 
  var body = "Name -  " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue();

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

  blob.setName("Name - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue() + ".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]
    });
}

我已经看过这样的脚本但是无法弄清楚如何让它在我的脚本上工作。

var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // orientation, false for landscape
  + '&fitw=true&source=labnol'           // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id

1 个答案:

答案 0 :(得分:1)

我有同样的问题,我也想删除边距。这是任何帮助,这是我的工作脚本,其中包括您在上面提到的部分。 但我没有看到一个可以调整边距的参数......



function CreaPDF() {

  var report = SpreadsheetApp.getActive();            
  var pdfName = "ReportXXX";
  var sheetName = "Sheet1";
  var sourceSheet = report.getSheetByName(sheetName);

  SpreadsheetApp.getActiveSpreadsheet().toast('Creating the PDF');

  // export url
  var url = 'https://docs.google.com/spreadsheets/d/'+report.getId()+'/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
  + '&size=A4'                           // paper size legal / letter / A4
  + '&portrait=false'                     // orientation, false for landscape
  + '&fitw=true'                        // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid='+sourceSheet.getSheetId();    // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  // request export url
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  var theBlob = response.getBlob().setName(pdfName+'.pdf');
  
  //var attach = {fileName:'Monthly Report.pdf',content:pdf, mimeType:'application/pdf'};
  
  var name = report.getRange("H1:H1").getValues();   // Get Name
  var emailTo = report.getRange("H2:H2").getValues();   // Get email
  var period = report.getRange("H3:H3").getValues();   // Get Reporting Period
  var subject = " - TEST Monthly Report - " + period;   // Construct the Subject Line
  var message = "Hi " + name + ", here is your latest report for " + period; // email body text

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


}