【问题标题】:Include sheet names when exporting Google Spreadsheets to PDF with Google Apps Scripts使用 Google Apps 脚本将 Google 电子表格导出为 PDF 时包含工作表名称
【发布时间】:2014-01-16 17:04:14
【问题描述】:

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

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 是否也支持这一点?

【问题讨论】:

    标签: google-apps-script google-apps


    【解决方案1】:

    .getAs() 方法不允许使用参数,但您可以使用电子表格 api,您可以在其中选择“普通”用户界面中的所有可用参数。 看这个post answer了解如何使用,关注github link

    这里是演示代码,因为 ref 中的代码有一些不一致之处。 (仅以导出带有网格和标题的 sheet1 为例进行说明)

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

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

    【讨论】:

    • 此信息现已过时,不再有此类方法。现在从表格创建pdf时自定义pdf的方法是什么?感谢有人更新。
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多