Home > Back-end >  Is there a way to get the exact Google Sheets page size in inches to use as a size parameter when sa
Is there a way to get the exact Google Sheets page size in inches to use as a size parameter when sa

Time:06-16

To generate the PDF, we can use combined inches to produce the page size:

  var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/'  
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'    //the file ID
      '/export?exportFormat=pdf&format=pdf'  
        '&size=7x6.7'  
          '&portrait=true'  
            '&fitw=true'   
              '&top_margin=0'              
                '&bottom_margin=0'           
                  '&left_margin=0'          
                    '&right_margin=0'       
                      '&sheetnames=false&printtitle=false'  
                        '&pagenum=false'  
                          '&gridlines=false'  
                            '&fzr=FALSE'  
                              '&gid='  
                                'XXXXXXXXXXXXXXXXXXXXXX'; //the sheet's ID

  var token = ScriptApp.getOAuthToken();
  var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer '    token } });
  var pdfBlob = docurl.getBlob();

  var fileName = 'test';
  var newFile = DriveApp.createFile(pdfBlob).setName(fileName);  

  if (DriveApp.getFoldersByName("archives").hasNext()){
    var folder = DriveApp.getFoldersByName("zovo").next();

  } else {
    var folder = DriveApp.createFolder("archives");
  }

  folder.addFile(newFile);
  DriveApp.removeFile(newFile);

Specifying the size in:

'&size=7x6.7'

PDF Result (white background and black cells):
enter image description here

But if any row or column changes its size, the page already loses its exact size and if it gets bigger it will go beyond the PDF page limit and if it gets smaller it will create an empty margin.

For example:

If any column shrinks in size, this will happen (white background and black cells):
enter image description here

If any lines decrease in size, this will happen (white background and black cells):
enter image description here

Is there a way to calc the size of a spreadsheet page in inches to use as a page size parameter to create a PDF?

enter image description here

After running the script, I got: enter image description here

Upon applying the values to the export link you provided, I got the following as a result:

enter image description here

References

You may browse the following references for more information:

  • Related