[英]Google UI Apps Script: Blank PDF when converting spreadsheet to PDF
[英]Why does Google Apps Script return a URL rather than a PDF file when I try to create PDF from a Spreadsheet?
與最近的這一問題[ 不允許從一個人訪問另一個Google電子表格 ](Q4-2014)不同,我想使用UrlFetchApp.fetch()而不是[file.getAs(MimeType.PDF)]從Google轉換一張工作表在我的Google雲端硬盤中將電子表格轉換為PDF。 UrlFetchApp.fetch()將允許我選擇橫向還是縱向以及其他幾個輸出控制參數。 file.getAs()缺少此類格式化選項。
我的問題是,使用UrlFetchApp.fetch(),我得到的文本文件包含PDF的URL,而不是PDF本身。
Example of the resulting file:
<HTML><HEAD><TITLE>Moved Temporarily</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>">"The document has moved
<A "HREF="https://docs.google.com/spreadsheets/d/1QTXXHEsOd9pZTfXxxxxxxxxxxphO4gn/export?"">"exportFormat=pdf&gid=1&gridlines=false&printtitle=false&size=A4&sheetnames=false&fzr=true&portrait=true&fitw=true">here</A>.
</BODY></HTML>
這是我的代碼(幾乎完全從Ben Oliver借來的https://gist.githubusercontent.com/benoliver999/9000157/raw/pdfmachine
function POtoDrive(){
var key = "1QTLGHEsOd9pZTfXM1xxxxxxxxxxxxxxxxxO4gntxPFTutYKQvE"; // Spreadsheet ID to create PDF from.
var pdf = spreadsheetToPDF(key); // Runs function below
var folder = DriveApp.getFoldersByName('newPdfs'); // A file does indeed get created in that folder
folder = folder.next();
folder.createFile(pdf); // Creates the PDF based on var below.
}
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 = {
"method": "GET",
"oAuthServiceName": "spreadsheets",
"oAuthUseToken": "always",
"muteHttpExceptions": true,
};
var doc = SpreadsheetApp.getActiveSpreadsheet();
var key = doc.getId();
var sheet = doc.getActiveSheet();
var name = "simplename.pdf";
// Makes PDF
var pdf = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+"&exportFormat=pdf&gid=1&gridlines=false&printtitle=false&size=A4&sheetnames=false&fzr=true&portrait=true&fitw=true", requestData).getBlob().setName(name);
return pdf;
}
如果將該URL放入瀏覽器,則可以下載一個看起來不錯的PDF。 但是,我希望腳本實際上將PDF文件拖放到我的Google雲端硬盤中。 好像我要獲取指針,或獲取PDF生成的URL,而不是PDF文件本身。 如何獲取要在Google雲端硬盤中創建的實際PDF文件?
這是我對Stackoverflow的第一個問題,因此請對禮節有任何違反。 謝謝。 :)
您的代碼沒有為新的電子表格使用正確的網址,以下是有效的版本:
function test(){
var id = '16vApdixgE6_________96QXEi1qvD58CSFJw';
var index = 0;
var name = 'test.pdf';
DriveApp.createFile(spreadsheetToPDF(id,index,name))
}
// Convert spreadsheet to PDF file.
function spreadsheetToPDF(id,index,name)
{
SpreadsheetApp.flush();
//define usefull vars
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://docs.google.com/feeds/";
//make OAuth connection
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
//get request
var request = {
"method": "GET",
"oAuthServiceName": "google",
"oAuthUseToken": "always",
"muteHttpExceptions": true
};
//define the params URL to fetch
var params = '?gid='+index+'&fitw=true&exportFormat=pdf&format=pdf&size=A4&portrait=true&sheetnames=false&printtitle=false&gridlines=false';
//fetching file url
var blob = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/"+id+"/export"+params, request);
blob = blob.getBlob().setName(name);
//return file
return blob;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.