2014-01-16 53 views
2

我正在使用Google Apps腳本將Google電子表格導出爲PDF格式,並希望在PDF頁面中包含「圖紙名稱」。這是可能的從代碼做到這一點?使用Google Apps腳本將Google電子表格導出爲PDF時包括圖表名稱

var spreadsheetFile = DocsList.getFileById(spreadsheet_id); 
var blob = spreadsheetFile.getAs('application/pdf'); 
blob.setName(spreadsheetFile.getName()); 
DocsList.getFolder(file_destination).createFile(blob); 

在電子表格應用程序中它支持在UI中,所以我想知道Apps腳本是否也支持這個功能? enter image description here

回答

2

.getAs()方法不允許使用參數,但您可以使用電子表格api,您可以在其中選擇「正常」用戶界面中可用的所有參數。 看到這個post answer看看如何使用它,並按照github link

這裏是演示代碼,因爲在代碼中有一些不一致的地方。 (只是爲了說明導出帶有網格和標題的工作表1的示例)

注意這將要求2個不同的授權。

function test(){ 
    var key = "0AnqSFd3iikE3dFd1WEVhMFhYczM5VWpuNDZHQ3AwZEE"; 
    var pdf = spreadsheetToPDF(key); 
    DocsList.createFile(pdf); 
} 

function spreadsheetToPDF(key) { 

    var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets"); 
    var scope = "https://spreadsheets.google.com/feeds" 

    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");  
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 

    var requestData = { 
    "oAuthServiceName": "spreadsheets", 
    "oAuthUseToken": "always", 
    }; 

    var name = DocsList.getFileById(key).getName()+".pdf"; 

    var pdf = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+ 
           "&exportFormat=pdf&gid=0&gridlines=true&printtitle=true&size=A4&sheetnames=true&fzr=true&portrait=true&fitw=true", requestData).getBlob().setName(name); 

    return pdf; 
} 

/* 
fmcmd=12 
size=legal/A4 
fzr=true/false 
portrait=false/true 
fitw=true/false 
gid=0/1/2 
gridlines=false/true 
printtitle=false/true 
sheetnames=false/true 
pagenum=UNDEFINED 
attachment=false/true 
*/ 
相關問題