2016-03-10 48 views
3

我有此腳本將電子表格的內容定期發送給所有協作者。導出爲PDF時更改文檔方向

function myFunction() { 
    var document = SpreadsheetApp.openById("123documentid456"); 

    var editors = document.getEditors(); 
    for(var i = 0; i < editors.length; i++){ 
    MailApp.sendEmail(editors[i].getEmail(), "Subject", "Some message", { 
     attachments : [document.getAs(MimeType.PDF)] 
    }); 
    } 
} 

它創建PDF並通過電子郵件發送。問題是內容不能很好地顯示,因爲PDF的方向是縱向。有沒有辦法讓它出口到景觀?

回答

1

here,這個代碼可以幫助

************************************************* 

function savePDFs() { 
    SpreadsheetApp.flush(); 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 

    var url = ss.getUrl(); 

    //remove the trailing 'edit' from the url 
    url = url.replace(/edit$/,''); 

    //additional parameters for exporting the sheet as a pdf 
    var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf 
    //below parameters are optional... 
    '&size=letter' + //paper size 
    '&portrait=false' + //orientation, false for landscape, true for portrait 
    '&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 
    '&gid=' + sheet.getSheetId(); //the sheet's Id 

    var token = ScriptApp.getOAuthToken(); 

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

    var blob = response.getBlob().setName(sheet.getName() + '.pdf'); 

    //from here you should be able to use and manipulate the blob to send and email or create a file per usual. 
    //In this example, I save the pdf to drive 

    DocsList.createFile(blob); 
    //OR DriveApp.createFile(blob); 
} 

************************************************* 

注意這一點:'&portrait=false' + //orientation, false for landscape, true for portrait

+0

完美。非常感謝 – Ubother