2017-07-31 50 views
0

我有基本的脚本做工精细,但它正是我想要它做的。 Google Sheet会将其转换为PDF并将其通过电子邮件发送给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 
+0

我不认为Google Apps脚本支持从提供的默认设置尚未预留比例为。您可以查看[云端硬盘服务](https://developers.google.com/apps-script/reference/drive/),但似乎没有提及此功能。 – noogui

回答

0

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

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

 

 
}

+0

你是什么意思*“这是任何帮助”*? – Pang

+0

这些是将边缘设置为0的参数: “top_margin = 0&left_margin = 0&right_margin = 0&bottom_margin = 0” – tmh