问题是,以这种方式生成的PDF文档具有不必要的不对称边距(右侧较大,左侧较窄)。我只想让页面居中。奇怪的是,当我从Google菜单文件->打印(或Ctrl + P)中打印时,文档会正确地居中显示。
我的代码如下:
function CreaPDF() {
//The function prints an invoice to PDF. First it copies spreadsheet to a new document.
//Deletes all sheet except the one to print. Saves it to PDF.
//It overwrites any existing doc with same name.
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sheetName = "Factura";
var folderID = getParentFolder(); // Folder id to save in a folder.
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
var numf = sourceSpreadsheet.getRangeByName("NumeroFactura").getValue();
var anof = numf.split("/",2); // Seeks number and year -> filename
var pdfName = anof[1] +"_Factura_" + anof[0]+ "_Dra_Salazar"; // Nombre del documento;
//Copy whole spreadsheet 2 temporary sheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
//Deletes pdf if already exists
var files = DriveApp.getFilesByName(pdfName);
while (files.hasNext()) {
files.next().setTrashed(true);
}
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1, 1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getDisplayValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
SpreadsheetApp.getActiveSpreadsheet().toast('Creando PDF');
//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
return true;
}
我打印时的设置如下:
- 纸张大小: "A4"
- 缩放比例: "正常" (我也试过:"适合宽度"和"适合页面")
- 方向: "竖向"
- 边距: "正常"
从Google帮助论坛中可以看出,谷歌打印的旧问题似乎仍然存在。总的来说,似乎无法保存打印设置,也没有办法传递任何参数给getAs('application/pdf')方法。因此,我认为该方法(以及菜单打印选项)使用的是默认参数,不能进行修改。有什么解决办法吗?"打印或更改页面设置"帮助页面并没有提供太多帮助。
非常感谢。