PDF margins - Google Script

自作多情 提交于 2019-12-11 17:12:33

问题


I have the basic script working fine, it does exactly what I want it to do. It takes the Google Sheet turns it into a PDF and emails me the PDF.

My question is how can I adjust the margins on the PDF, I need to set the PDF to fit page. I can't just resize the sheet because it throws the spacing off.

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

I've seen script like this but cant figure out how to get it to work on my script.

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:


I've got the same issue, I also want to remove the margins. It is is any help, here is my working script, which includes the part you are mentioning above. But I don't see a parameter where you can adjust the margins...

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


}


来源:https://stackoverflow.com/questions/45422008/pdf-margins-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!