0

我正在使用已在各种线程中显示的代码,例如Google 应用程序脚本 getAs('application/pdf') 布局

问题是导出的 PDF 不包含 url 导出参数中提到的页码。其余所有参数均已导出。

我的代码如下:

function createblobpdf(sheetName, pdfName) {
  var sourceSpreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
  var url = 'https://docs.google.com/spreadsheets/d/' + sourceSpreadsheet.getId() + '/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
    +    '&size=A4' // paper size legal / letter / A4
    +    '&portrait=true' // orientation, false for landscape
    +    '&fitw=true' // fit to page width, false for actual size
    +    '&sheetnames=true&printtitle=false' // hide optional headers and footers
    +    '&pagenum=true&gridlines=false' // hide page numbers and gridlines
    +    '&fzr=false' // do not repeat row headers (frozen rows) on each page
    +    '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
    +    '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM
    +    '&gid=' + sourceSheet.getSheetId(); // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  // request export url
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });

  var theBlob = response.getBlob().setName(pdfName);

  return theBlob;
};

我尝试了以下参数pagenum=true&&pagenumbers=truepagenumber=true

请帮忙。

4

2 回答 2

0

看来目前这个参数不起作用。

我在Google 的问题跟踪器上发现了一个与此类似的问题,您可以单击星号以获取有关它的更新。

问题跟踪器答案的更新:

官方不支持使用此方法将 Google 表格导出为 PDF。使用的参数未记录在案,如有更改,恕不另行通知。

目前,Drive API Files: export是 Web UI 之外唯一支持的导出方法。如果您希望在导出设置中获得更多粒度,请考虑提交功能请求。

于 2020-11-03T14:33:57.807 回答
0

最后我得到了我自己问题的答案。现在显示页码,但不能同时显示任何文本,并且页码的位置固定在 PDF 的页面底部。

要传递的参数是:

'&pagenum=RIGHT

其他参数可以是'LEFT','CENTER'

'&pagenum=TRUE将 PDF 导出为 Drive API 不接受TRUE作为页码的可识别参数时,这些参数不起作用。

于 2020-12-06T10:29:47.810 回答