【问题标题】:exporting spreadsheet to pdf then saving the file in google drive将电子表格导出为 pdf,然后将文件保存在谷歌驱动器中
【发布时间】:2012-10-14 10:51:59
【问题描述】:

我在将 Google 电子表格打印为 pdf 后保存到 Google Drive 时遇到问题。 如果我只是将“printurl”字符串放入浏览器,它会自动给我文件。但我希望它自动保存到谷歌驱动器。我通过从其他将电子表格作为 PDF 发送电子邮件的帖子中借用代码来尝试此代码。但这会产生一个无法打开的pdf。我究竟做错了什么?

function printpdf() {
var spreadsheet_id="0Aiy1DTQRndx6dDRidXoxNzlXZFhxd2FITTlBbnUybnc";
var settings = '&fitw=true&portrait=false&exportFormat=pdf&gid=0&gridlines=false';
var printurl = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?   key=' + spreadsheet_id + settings;
var result=UrlFetchApp.fetch(printurl);
var content=result.getContent();
var file=DocsList.createFile("temp",content,"application/pdf");
}

这是在新的 oauth2 格式下对此问题的更新。

Printing spreadsheet to PDF then saving file in Drive using OAuth2

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    你可以用更简单的方式做到这一点

    function printpdf(){
    
      var spreadsheet_id="0Aiy1DTQRndx6dDRidXoxNzlXZFhxd2FITTlBbnUybnc";
      var spreadsheetFile = DocsList.getFileById(spreadsheet_id); 
      var blob = spreadsheetFile.getAs('application/pdf'); 
      DocsList.createFile(blob);
    }
    

    请注意,DocsList.createFile(blob) 仅适用于 Google Apps 帐户。

    【讨论】:

    • 嗨,斯里克,感谢您的回复。我已经试过了。实际上,这是我正在使用的当前代码,但是,我无法指定诸如纵向或横向、网格或无网格之类的选项。如何使用您的方法并保持灵活性?
    • 目前没有可用的选项来增强/修改 pdf 导出,有一个 open issue on this
    • @Srik。使用新的谷歌表格,您可以使用您的方法,但可以使用其他设置,例如纵向/横向等?
    【解决方案2】:

    你是这么说的吗?

      var id = SpreadsheetApp.getActiveSpreadsheet().getId();
      var sheetName = getConfig(SHEET_NAME_CELL);
      var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    
      if (!dataSheet) {
        Browser.msgBox("Can't find sheet named:" + sheetName);
        return;
      }
    
      var dataSheetIndex = dataSheet.getSheetId();
    
     //this is three level authorization 
      var oauthConfig = UrlFetchApp.addOAuthService("google");
      oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
      oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oauthConfig.setConsumerKey("anonymous");
      oauthConfig.setConsumerSecret("anonymous");
    
      //even better code
      //oauthConfig.setConsumerKey(ScriptProperties.getProperty("consumerKey"));
      //oauthConfig.setConsumerSecret(ScriptProperties.getProperty("consumerSecret"));
    
      var requestData = {
        "method": "GET",
        "oAuthServiceName": "google",
        "oAuthUseToken": "always"
      };
    
    
      var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + id +  "&gid=" + dataSheetIndex + "&fitw=true&size=A4&portrait=true&sheetnames=false&printtitle=false&exportFormat=pdf&format=pdf&gridlines=false";
    
    
    
      //Save File to Google Drive 
      var seplogoBlob = UrlFetchApp.fetch(url, requestData).getBlob().setName("Filename.pdf");
      DocsList.createFile(seplogoBlob);
    

    【讨论】:

    • 不确定您是在问这是正确的方法还是发布实际的解决方案。你能澄清一下吗?
    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多