首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于将图纸导出为pdf的URL缩放参数

用于将图纸导出为pdf的URL缩放参数
EN

Stack Overflow用户
提问于 2019-11-10 09:37:17
回答 1查看 519关注 0票数 1

另一个关于URL参数的问题...抱歉的。实际上,我已经搜索了很多关于如何解决这个问题的方法,无论是在这里还是在其他地方,我都没有找到任何东西。零!

如果有人能帮我一把真的很感谢。事情是这样的,我正在用下面的URL代码将一个google表格导出到pdf。

代码语言:javascript
复制
    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中都会以相同的大小导出。

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

我缺少的是如何包含它。

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

发布于 2019-11-13 23:40:29

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

代码语言:javascript
复制
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
            ]
        ]
    );
}

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

代码语言:javascript
复制
var ui = SpreadsheetApp.getUi();
ui.createMenu('X Menu')
    .addItem('Criar gráficos', 'resultAutomation')
    .addSeparator()
    .addItem('Print graph', 'myExportPDF') // NEW
    .addToUi();

参考文献

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58785063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档