问题
Another question on URL parameters... sorry. Actually, I've been searching a lot about how to solve this, here and pretty much everywhere else, and I found absolutely nothing. Zero!
Really appreciate if someone can give me a hand. Here's the thing, I'm exporting a google sheet to pdf with the following URL code.
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";
It works just fine, but my problem is that I'd like to add a 130% zoom. Just to save you the trouble, I know and tried the 'scale' parameter, but that's not what I'm looking for. It might help the hero who accepts to save me from this hell :-) to know that I'm exporting a graphic and that the 'scale' parameter doesn't have any effect on the pdf. Meaning: no matter which value I choose for the 'scale' parameter, the graphic is exported with the same exact size in the A4 pdf.
I tried to export it manually to pdf with that 130% zoom I need using the printing dialog box, which I'm guessing sets exactly the same parameters we work with here, so I believe it should be possible to use the same parameter in an URL.
What I'm missing is how to include it.
Can anyone help?
回答1:
By tracking down the requests that Sheets is internally doing (and checking the link below), I have come up to the following solution:
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
]
]
);
}
This code will download your "Resultado" Sheet as PDF with the format you expect. Furthermore, you can add this code to your menu by changing the onOpen function as follows:
var ui = SpreadsheetApp.getUi();
ui.createMenu('X Menu')
.addItem('Criar gráficos', 'resultAutomation')
.addSeparator()
.addItem('Print graph', 'myExportPDF') // NEW
.addToUi();
Reference
- Export Google Sheets to PDF
来源:https://stackoverflow.com/questions/58785063/url-zoom-parameter-for-exporting-sheet-to-pdf