【问题标题】:Why does Google Apps Script return a URL rather than a PDF file when I try to create PDF from a Spreadsheet?当我尝试从电子表格创建 PDF 时,为什么 Google Apps 脚本会返回 URL 而不是 PDF 文件?
【发布时间】:2014-11-02 17:42:16
【问题描述】:

与最近一期 [Not allow to access from one to other Google Spreadsheet](2014 年第四季度)不同,我想使用 UrlFetchApp.fetch() 而不是 [file.getAs(MimeType.PDF)] 将 Google 电子表格中的单个工作表转换为我的 Google Drive 中的 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/pdfmachineBen 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 Drive。就好像我正在取回一个指针,或者取回一个 PDF 生成 URL,而不是 PDF 文件本身。如何在我的 Google Drive 中创建实际的 PDF 文件?

这是我在 Stackoverflow 上的第一个问题,因此请对任何违反礼节的行为表示友好。谢谢。 :)

【问题讨论】:

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


【解决方案1】:

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

(借用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;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
相关资源
最近更新 更多