使用 Google Apps 脚本将 Google 电子表格导出为 PDF 时包括工作表名称 [英] Include sheet names when exporting Google Spreadsheets to PDF with Google Apps Scripts

查看:21
本文介绍了使用 Google Apps 脚本将 Google 电子表格导出为 PDF 时包括工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Apps 脚本将 Google 电子表格导出为 PDF 格式,并希望在 PDF 页面上包括工作表名称".这可以从代码中做到这一点吗?

I am exporting a Google Spreadsheet to PDF format with Google Apps Script, and would like to 'include sheet names' on the PDF pages. Is this possible to do this from the code?

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

在电子表格应用中,UI 支持它,所以我想知道 Apps Scripts 是否也支持这个?

In the Spreadsheet app it is supported in the UI, so I am wondering whether Apps Scripts supports this too?

推荐答案

.getAs() 方法不允许使用参数,但您可以使用电子表格 API,您可以在其中选择所有可用的参数来自正常"的用户界面.请参阅此 发表答案 以了解如何使用它,并按照 github链接

The .getAs() method does not allow for parameters but you can use the spreadsheet api where you can choose all the parameters available from the "normal" Ui. See this post answer to see how to use it, and follow the github link

这是演示代码,因为参考文献中的代码存在一些不一致之处.(只是为了举例说明导出带有网格和标题的 sheet1)

Here is the demo code as there were a few inconsistencies in the code in ref. (just to illustrate with an example exporting sheet1 with grid and title)

请注意,这将要求 2 个不同的授权.

note this will ask for 2 distinct authorizations.

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  
*/

这篇关于使用 Google Apps 脚本将 Google 电子表格导出为 PDF 时包括工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆