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

当我尝试从电子表格创建PDF时,为什么Google Apps脚本会返回URL而不是PDF文件?,pdf,google-apps-script,google-spreadsheet-api,urlfetch,Pdf,Google Apps Script,Google Spreadsheet Api,Urlfetch,与最近一期[](2014年第4季度)不同,我想在我的谷歌硬盘中使用UrlFetchApp.fetch()而不是[file.getAs(MimeType.PDF)]将谷歌电子表格中的一张工作表转换为PDF格式。UrlFetchApp.fetch()允许我选择横向与纵向以及其他几个输出控制参数。file.getAs()缺少此类格式选项 我的问题是,使用UrlFetchApp.fetch(),我会返回一个包含PDF URL的文本文件,而不是PDF本身 Example of the resulting

与最近一期[](2014年第4季度)不同,我想在我的谷歌硬盘中使用UrlFetchApp.fetch()而不是[file.getAs(MimeType.PDF)]将谷歌电子表格中的一张工作表转换为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> 
如果我把这个网址放进我的浏览器,我可以下载一个看起来不错的PDF。但是,我希望脚本能够将一个PDF文件放入我的谷歌硬盘。这就好像我要返回一个指针,或者返回一个生成PDF的URL,而不是PDF文件本身。如何在我的谷歌硬盘中创建实际的PDF文件


这是我关于Stackoverflow的第一个问题,所以请对任何违反礼仪的行为表示友好。谢谢。:)

您的代码没有为新的电子表格使用正确的url,下面是一个工作版本:

(借用)


看看这篇文章:请注意,OAuthConfig不再受支持!有关工作版本,请参见示例。。。
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;
}
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;
}