Building link to export sheet as PDF

南楼画角 提交于 2019-12-11 02:31:26

问题


I've been trying to export specific sheets as PDFs for mailing purposes, and found that I needed them to be in a specific format. I went around looking for code to use and ended up finding this: https://issuetracker.google.com/issues/36761058#comment24

Although I have incorporated quite a bit of my own code, a good chunk of it is the same as in the link I posted above, and it works quite well except for one thing.

As seen in the code below, I've chosen to have fit width as "false" since some of my sheets have a lot of columns. However, the PDFs I get are always in the fit to width format, and not actual size, making it practically impossible to read should it be printed. I've tried to look into it, but my attempts so far have turned up empty-handed, so I've finally decided to try and ask here.

     var url = ss.getUrl(); 


  //remove the trailing 'edit' from the url 
  url = url.replace(/edit$/,''); 

  //additional parameters for exporting the sheet as a pdf 
  var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf 
  //below parameters are optional... 
  '&size=A4' + //paper size 
  '&portrait=false' + //orientation, false for landscape 
  '&fitw=false' + //fit to width, false for actual size 
  '&sheetnames=true&printtitle=true&pagenumbers=true' + //hide optional headers and footers 
  '&gridlines=true' + //hide gridlines 
  '&fzr=false' + //do not repeat row headers (frozen rows) on each page 
  '&gid=' + spaceman.getSheetId();

  var token = ScriptApp.getOAuthToken(); 
    Logger.log("URL: " + url + url_ext);

  var response = UrlFetchApp.fetch(url + url_ext, { 
      "muteHttpExceptions": true,
      headers: { 
        'Authorization': 'Bearer ' +  token 
      } 
    }); 

Any help or suggestions on how to solve this issue of mine would be greatly appreciated. Of course, if something needs to be clarified or added to the original post then I would be happy to do so.

EDIT: I've been really stuck on this so I haven't really done anything else to the script, just tested the script again today and suddenly I'm getting actual size instead of fitw, so I suppose it could've been a bug?


回答1:


I am not sure if this is a bug- But if you access the URL direct, you can replicate your issue-

https://docs.google.com/spreadsheets/d/ADDYOURIDSSHERE/export?fitw=false&format=pdf


来源:https://stackoverflow.com/questions/44457298/building-link-to-export-sheet-as-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!