Google Apps Script: DocumentApp setPageWidth, setPageHeight
Sometimes you need to prepare a Google Doc’s paper size and orientation programmatically using Google Apps Script.
Unfortunately, you can’t just call for say, A4 in Landscape. Okay, not until now (see my code below).
Google Apps Script does provide a way to set the dimensions of your page in the body class by using:
setPageWidth(pageWidth)
setPageHeight(pageHeight)
The page widths and heights are measured in PostScripts Points which is a bit of a pain too.
Here is an example of setting and A3 paper size in Landscape.
1 2 |
var document = DocumentApp.getActiveDocument(); document.getBody().setPageHeight(841.89).setPageWidth(1190.55); |
Ugh. What a chore. You need to find the dimensions of the paper in points.
Enter this little nifty function and your life will be so much easier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/** * Sets the Paper Size and Orientation * * Paper sizes match Google Docs <Page Setup> * * INPUTS * -argument- :-inches- :-mm- :-points- * letter_size :8.5"x11" :216x279 :612.283x790.866 * tabloid_size :11"x17" :279x432 :790.866x1224.57 * legal_size :8.5"x14" :216x356 :612.283x1009.13 * statement_size :5.5"x8.5" :140x216 :396.85x612.283 * executive_size :7.25"x10.5" :184x267 :521.575x756.85 * folio_size :8.5"x13" :216x330 :612.283x935.433 * a3_size :11.69"x16.54" :297x420 :841.89x1190.55 * a4_size :8.27"x11.69" :210x297 :595.276x841.89 * a5_size :5.83"x8.27" :148x210 :419.528x595.276 * b4_size :9.84"x13.9" :250x353 :708.661x1000.63 * b5_size :6.93"x9.84" :176x250 :498.898x708.661 * * ORIENTATION * Set to portriat. To change to landscape set second argument to false. * * EXAMPLE * The function takes 3 arguments: * 1. Document e.g. var document = DocumentApp.getActiveDocument(); * 2. Paper Size e.g. letter_size, a5_size * 3. Portrail or not as a boolean(true, false) false = Landscape * * --Tabloid in Portrait-- * var document = DocumentApp.getActiveDocument(); * var tabloid = paper_size(document,"tabloid_size",true); * * --A4 in Landscape-- * var document = DocumentApp.getActiveDocument(); * var A4_landscape = paper_size(document,"a4_size", false); */ function paper_size(doc, paperSize, portrait) { //Dictionary of Paper sizes by width and height in portrait. var paper = { letter_size:[612.283,790.866], tabloid_size:[790.866,1224.57], legal_size:[612.283,1009.13], statement_size:[396.85,612.283], executive_size:[521.575,756.85], folio_size:[612.283,935.433], a3_size:[841.89,1190.55], a4_size:[595.276,841.89], a5_size:[419.528,595.276], b4_size:[708.661,1000.63], b5_size:[498.898,708.661]}; if(portrait===true){ doc.getBody().setPageWidth(paper[paperSize][0]).setPageHeight(paper[paperSize][1]); }else if(portrait === false){ doc.getBody().setPageHeight(paper[paperSize][0]).setPageWidth(paper[paperSize][1]); }else{ Logger.log("No boolean selected"); return; }; }; |
You can see that the dictionary of paper sizes matches the options in File>Page Setup...>Paper Size
The function takes 3 parameters:
- The Document variable. This can be a newly created document, and active document or one you call by id.
- The Paper Size. Identified by the name of the paper type followed by an underscore and the size. E.g.
legal_size
. - Portrait or Landscape. Here a boolean is used. If true, then Portrait will be used. If false, then the page will be set to Landscape.
The function call would look like this:
var letterLandscape = paper_size(document,"letter_size", false);
If you would prefer to do your own thing or you will only need to make one page, layout in your code then it might be best to use the setPageHeight(points)
, setPageWidth(points)
, methods and use the reference table below:
Paper Size | Argument | Inches | Millimeters | Points |
Letter | letter_size | 8.5″x11 | 216×279 | 612.283×790.866 |
Tabloid | tabloid_size | 11″x17″ | 279×432 | 790.866×1224.57 |
Legal | legal_size | 8.5″x14″ | 216×356 | 612.283×1009.13 |
Statement | statement_size | 5.5″x8.5″ | 140×216 | 396.85×612.283 |
Executive | executive_size | 7.25″x10.5″ | 184×267 | 521.575×756.85 |
Folio | folio_size | 8.5″x13″ | 216×330 | 612.283×935.433 |
A3 | a3_size | 11.69″x16.54″ | 297×420 | 841.89×1190.55 |
A4 | a4_size | 8.27″x11.69″ | 210×297 | 595.276×841.89 |
A5 | a5_size | 5.83″x8.27 | 148×210 | 419.528×595.276 |
B4 | b4_size | 9.84″x13.9″ | 250×353 | 708.661×1000.63 |
B5 | b5_size | 6.93″x9.84″ | 176×250 | 498.898×708.661 |
Finally, if you are looking to convert your own special page setup, then Google can do that for you. A simple keyword search like “milometers to points” will bring up Google’s unit converter.
If you want to make conversions in the code then:
- 1 mm = 2.834646 point
- 1 Inch = 72 Points
Hope you find this useful.
Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get you from the basics to a real Google Apps Script pro.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Make an enquiry on my 'Hire me!' page. I occasionally pick up projects. Alternatively, Fiverr’s your best bet to find a skilled Google Apps Script developer to solve your problem quickly and professionally. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.