1

I am using the code as already displayed in various thread such as Google apps script getAs('application/pdf') layout

The problem is that the exported PDF does not contain page number as mentioned in the url export parameters. Rest all parameters are exported.

My code is as below:

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;
};

I have tried the following parameters pagenum=true & pagenumbers=true & pagenumber=true

Please help.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Harsh
  • 209
  • 4
  • 12
  • 1
    Looks like a Sheets bug. Have you tried different answer: https://stackoverflow.com/a/57187650/555121 ? – Kos Nov 02 '20 at 22:31
  • I saw that answer but the parameters mentioned in that answer are not found anywhere so don't know which null to be replaced with my values. Trying to get it done in an easier way. – Harsh Nov 03 '20 at 04:12

2 Answers2

1

Finally I got an answer to my own question. The page number is now displayed but one can not display any text along with it and the position of the same is fixed to the bottom of the page in the PDF.

The parameter to be passed is:

'&pagenum=RIGHT

The other parameters can be 'LEFT', 'CENTER'

The paraments '&pagenum=TRUE does not work while exporting PDF as Drive API does not accept TRUE as recognized parameter for page numbers.

Harsh
  • 209
  • 4
  • 12
0

It seems that currently this parameter does not work.

I have found an issue similar to this one on Google's Issue tracker you can click on the star to get updates on it.

Update on the issue tracker answer:

Exporting a Google Sheets to PDF with this method is not officially supported. The parameters used are not documented and subject to change without notice.

Currently, Drive API Files: export is the only supported export method outside of the web UI. Please consider filing a Feature Request if you'd like to have more granularity in export settings.

Kessy
  • 1,894
  • 1
  • 8
  • 15