exporting spreadsheet to pdf then saving the file in google drive

前端 未结 2 978
北荒
北荒 2021-01-06 19:28

I\'m having problems saving to google drive after printing a google spreadsheet to pdf. If i just put the \"printurl\" string into a browser, it will automatically give me t

相关标签:
2条回答
  • 2021-01-06 19:43

    You can do it in a much simpler fashion

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

    Note that the DocsList.createFile(blob) works only with Google Apps accounts.

    0 讨论(0)
  • 2021-01-06 19:58

    did you mean it like that?

      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);
    
    0 讨论(0)
提交回复
热议问题