当我尝试从电子表格创建PDF时,为什么Google Apps脚本会返回网址而不是PDF文件?

时间:2014-11-02 17:42:16

标签: pdf google-apps-script google-spreadsheet-api urlfetch

与最近的问题[Not allow to access from one to other Google Spreadsheet](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&amp;gid=1&amp;gridlines=false&amp;printtitle=false&amp;size=A4&amp;sheetnames=false&amp;fzr=true&amp;portrait=true&amp;fitw=true">here</A>.
</BODY></HTML> 

这是我的代码(几乎完全是在https://gist.githubusercontent.com/benoliver999/9000157/raw/pdfmachine

从Ben Oliver那里借来的
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的第一个问题,所以请善待任何违反礼仪的行为。谢谢。 :)

1 个答案:

答案 0 :(得分:0)

您的代码未使用新网址的正确网址,以下是有效版本:

(借用from this post

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