Export to PDF with parameters

半城伤御伤魂 提交于 2019-12-11 05:33:53

问题


Been reading about how to save sheet to a PDF file and I stumble to this code it works well before but now it won't been getting error of "uiapp deprecated". How can I fix this? BTW the codes not mine,I just tweak it to suit my preferences. It's suggesting to use HTML Services.

function PDF() {
    SpreadsheetApp.flush();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var gid = sheet.getSheetId();
    var pdfOpts = '&size=letter&fzr=false&portrait=true&fitw=false&gridlines=false&scale=1&top_margin=0.17&bottom_margin=0.17&left_margin=0.1&right_margin=0.1&printtitle=false&sheetnames=false&pagenum=UNDEFINED&attachment=false&gid='+gid;
    var row2 = 66;
    var printRange = '&c1=0' + '&r1=0' + '&c2=15' + '&r2='+row2; // B2:APn
    var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts + printRange;
    var app = UiApp.createApplication().setWidth(300).setHeight(100);
    app.setTitle('Print this sheet');
    var link = app.createAnchor('Download PDF', url).setTarget('_new');
    app.add(link);
    ss.show(app);
};

回答1:


How about this modification? I think that there might be several answers for your situation. So please think of this as just one of them.

Modified script:

In this modification, I used the method of showModalDialog of Class Ui.

From:
var app = UiApp.createApplication().setWidth(300).setHeight(100);
app.setTitle('Print this sheet');
var link = app.createAnchor('Download PDF', url).setTarget('_new');
app.add(link);
ss.show(app);
To:
var html = '<a href="' + url.replace(/&/g, "&amp;") + '" target="_new">Download PDF</a>';
var userInterface = HtmlService.createHtmlOutput(html).setWidth(300).setHeight(100);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Print this sheet');

Reference:

  • Class Ui

If I misunderstood your question, please tell me. I would like to modify it.



来源:https://stackoverflow.com/questions/54375744/export-to-pdf-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!