问题
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
回答1:
This was also reported in this thread. I suggest to file a feature request for this.
回答2:
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;
回答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
回答4:
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.
来源:https://stackoverflow.com/questions/46043420/google-apps-script-print-sheet-with-margins