PDF页边距-Google脚本 [英] PDF margins - Google Script

查看:87
本文介绍了PDF页边距-Google脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本脚本工作正常,它确实可以实现我想要的功能.Google表格需要将其转换为PDF,然后通过电子邮件将其发送给我.

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.

我的问题是如何调整PDF的页边距,我需要将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

推荐答案

我遇到了同样的问题,我也想删除空白.有什么帮助,这是我的工作脚本,其中包括您上面提到的部分.但是我看不到可以调整边距的参数...

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


}

这篇关于PDF页边距-Google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆