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):
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):

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

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):

After running the script, I got:

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

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!