2

There is a nice GAS script to print a Google sheet in google app script print button

As discussed on the Google Cloud Connect (https://www.cloudconnect.goog/message/77627), we are trying to identify a way to control the margins.

There is also a similar question over on the Google Product Forums at https://productforums.google.com/forum/#!topic/docs/DQxnJwoDn0c

Rubén
  • 34,714
  • 9
  • 70
  • 166
Kevin A. McGrail
  • 433
  • 1
  • 5
  • 7

4 Answers4

3

You might find this script useful.

I think that it might answer your question on the margins plus it has a few other helpful settings that you can play with.

function printPdf() {
    SpreadsheetApp.flush();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var gid = sheet.getSheetId();
    var pdfOpts = '&size=A3&fzr=true&portrait=false&fitw=true&gridlines=false&printtitle=true&sheetnames=true&pagenumbers=true&attachment=false&gid='+gid;
    var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts
    var app = UiApp.createApplication().setWidth(300).setHeight(100);
    app.setTitle('Your Print Preview is Ready');
    var link = app.createAnchor('Open Print Preview', url).setTarget('_new');
    app.add(link);
    ss.show(app);
}

See also: Printing a sheet to PDF

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
2

This was also reported in this thread. I suggest to file a feature request for this.

abielita
  • 13,147
  • 2
  • 17
  • 59
1

Your first link uses the UiApp which has been depricated but I think I'd look in the following if you still wish to use it.

var pdfOpts = '&size=A4&fzr=false&portrait=false&fitw=true&gridlines=false&printtitle=false&shee        tnames=false&pagenum=UNDEFINED&attachment=false&gid='+gid;
Cooper
  • 59,616
  • 6
  • 23
  • 54
0

it is (currently) neither possible through App Script API ...nor the Macro Recorder

therefore, I'd suggest to utilize some Chrome extension, alike Automation.

the key-codes to open the page setup dialog and enter the desired values and then proceed to hit the Next button, should be about alike this: <Alt> + F, P, (select margins, then select custom numbers, then select the top margin entry field, that can be reached by <Tab>, <Cursor Down> and <Space>)... and then 0.25", <Tab>, 0.25", <Tab>, 0.25", <Tab>, 0.25" ... <Tab>, <Tab>, <Enter>.

from within a spreadsheet (and not even from within client-side JavaScript), without any browser extension, which can send key-codes, there is no access to these properties - because otherwise scripts could easily mess up custom local printer settings.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216