用于将工作表导出为pdf的URL缩放参数

问题描述 投票:2回答:1

关于URL参数的另一个问题...对不起。实际上,在这里以及几乎所有其他地方,我一直在寻找有关如何解决此问题的很多方法,但是我什么也没发现。零!

非常感谢有人可以帮我。这就是问题,我正在使用以下网址代码将Google工作表导出为pdf。

    var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=pdf&"+
                                                        "size=7&"+
                                                        "portrait=false&"+
                                                        // "zoom_scale=130&"+
                                                        "top_margin=0.00&"+
                                                        "bottom_margin=0.00&"+
                                                        "left_margin=0.00&"+
                                                        "right_margin=0.00&"+
                                                        "horizontal_alignment=LEFT&"+
                                                        "vertical_alignment=TOP";
                                                        //"gridlines=false&";
                                                        //"printtitle=false&"+
                                                        //"sheetnames=false&"+
                                                        //"pagenum=UNDEFINED&"+
                                                        //"attachment=false";

效果很好,但我的问题是我想添加130%的缩放比例。为了节省您的麻烦,我知道并尝试过“ scale”参数,但这不是我想要的。这可能有助于接受这个英雄的英雄:-)知道我正在导出图形,并且'scale'参数对pdf没有任何影响。含义:无论我为“ scale”参数选择哪个值,图形都将以相同的精确尺寸导出到A4 pdf中。

我尝试使用打印对话框以130%的缩放比例将其手动导出为pdf,我猜这将设置与此处使用的参数完全相同的参数,因此我相信应该可以使用相同的参数在网址中。

我缺少的是如何包含它。

有人可以帮忙吗?

url google-apps-script google-sheets export-to-pdf
1个回答
0
投票

通过跟踪Sheets内部正在执行的请求(并检查下面的链接),我得出了以下解决方案:

function encodeDate(yy, mm, dd, hh, ii, ss) {
    var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (((yy % 4) == 0) && (((yy % 100) != 0) || ((yy % 400) == 0))) days[1] = 29;
    for (var i = 0; i < mm; i++) dd += days[i];
    yy--;
    return ((((yy * 365 + ((yy - (yy % 4)) / 4) - ((yy - (yy % 100)) / 100) + ((yy - (yy % 400)) / 400) + dd - 693594) * 24 + hh) * 60 + ii) * 60 + ss) / 86400.0;
}

function exportPDF(ssID, source, options, format) {
    var dt = new Date();
    var d = encodeDate(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds());
    var pc = [null, null, null, null, null, null, null, null, null, 0,
        source,
        10000000, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
        d,
        null, null,
        options,
        format,
        null, 0, null, 0
    ];
    var js = " \
<script> \
window.open('https://docs.google.com/spreadsheets/d/" + ssID + "/pdf?id=" + ssID + "&a=true&pc=" + JSON.stringify(pc) + "&gf=[]'); \
google.script.host.close(); \
</script> \
";

    var html = HtmlService.createHtmlOutput(js)
        .setHeight(10)
        .setWidth(100);
    SpreadsheetApp.getUi().showModalDialog(html, "Save To PDF");
}

function myExportPDF() {
    var as = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = as.getSheetByName('Resultado');
    exportPDF(as.getId(), // Spreadsheet ID
        [
            [sheet.getSheetId().toString()] // Sheet ID
        ],
        [
            0, // Do not show notes
            null,
            1, // Show grid lines
            0, // Do not show page numbers
            0, // Do not show book title
            0, // Do not show sheet title
            0, // Do not show date
            0, // Do not show time
            1, // Repeat pinned rows
            1, // Repeat pinned columns
            1, // Page order down then up
            2,
            null,
            [
                null,
                null,
                ["kocheam.com"]
            ],
            1, // Left Alignment
            2 // Top Alignment
        ],
        [
            "A4", // A4 sheet format
            0, // Page Orientation Vertical
            5, // Align to height
            1.3, // Zoom
            [
                0, // Top margin 0.75 inch
                0, // Bottom margin 0.75 inch
                0, // Left margin 0.7 inch
                0 // Right margin 0.7 inch
            ]
        ]
    );
}

此代码将以您期望的格式将您的“ Resultado”表格下载为PDF。此外,您可以通过如下更改onOpen函数将此代码添加到菜单中:

var ui = SpreadsheetApp.getUi();
ui.createMenu('X Menu')
    .addItem('Criar gráficos', 'resultAutomation')
    .addSeparator()
    .addItem('Print graph', 'myExportPDF') // NEW
    .addToUi();

参考

© www.soinside.com 2019 - 2024. All rights reserved.