• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Is there a way to get the exact Google Sheets page size in inches to use as a size parameter when saving as PDF?

Resolved: Is there a way to get the exact Google Sheets page size in inches to use as a size parameter when saving as PDF?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

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?
Example sheet

Answer:

Script


You can use the script below wherein it computes for the total length and width of the non-empty cells in a Google Spreadsheet.
function getsize() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lc = ss.getLastColumn();
  var colWidth = 0;
  for (i=1; i<=lc; i++) {
    colWidth += ss.getColumnWidth(i);
  }
  var width = colWidth / 96;
  console.log("Width = ", width);
  var lr = ss.getLastRow();
  var rowHeight = 0;
  for (i=1; i<=lr; i++) {
    rowHeight += ss.getRowHeight(i);
  }
  var height = rowHeight / 96;
  console.log("Length = ", height)
}
The script uses two iterations which sums up the total length and total width of the spreadsheet. This is possible with the use of the getColumnWidth() and getRowHeight() functions which measure the width and height of the columns and rows, respectively, in pixels.
NOTE: The border thickness is not included in the calculation of getColumnWidth() and getRowHeight().

Conversion


After getting the total width and height in pixels, the values are converted into inches by using the proportion:

1 inch = 96 pixels


Sample


I made a test case (10 columns by 9 rows): 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:
  • Comprehensive guide ๐Ÿ““: Export Google Sheets as PDF, Excel, CSV, or other formats using Apps Script
  • getColumnWidth()
  • getRowHeight()

If you have better answer, please add a comment about this, thank you!

google-apps-script google-sheets
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Resolved: How to scroll bottom of div at launch?

02/04/2023

Resolved: how to get and read an xml file in a zip file using xml.etree

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.