[英]File size increases when converting a Google Sheet into PDF format in Apps script
I have created a script where in Google Sheet is converted to PDF and sent in an email (via Gmail API).我创建了一个脚本,其中在 Google Sheet 中转换为 PDF 并在 email 中发送(通过 Gmail API)。 for certain records, the PDF size gets increased up to 10 MB (with script) and actually the file size is 130 KB when downloaded manually.对于某些记录,PDF 大小增加到 10 MB(带脚本),手动下载时文件大小实际为 130 KB。
Please find the below syntax which is used to convert sheet to PDF请找到以下用于将工作表转换为 PDF 的语法
var blob=DriveApp.getFileById(<<Google Sheet ID>>).getAs('application/pdf');
What could be the possible reason for this issue and How to resolve it?此问题的可能原因是什么以及如何解决?
I recommend you instead to convert in a more manual way, with the UrlfetchApp
- this allows you to specify different options, among others the range you want to convert.我建议您使用UrlfetchApp
以更手动的方式进行转换 - 这允许您指定不同的选项,其中包括您想要转换的范围。
The following sample shows how to export only the data with contents ( dataRange
) for a spreadsheet that contains onlyone sheet:以下示例显示了如何仅导出包含仅包含一个工作表的电子表格的内容 ( dataRange
) 的数据:
function myFunction() {
var token = ScriptApp.getOAuthToken();
var ss = SpreadsheetApp.openById("<<Google Sheet ID>>");
var sheet = ss.getActiveSheet();
var name = ss.getName();
var url = "https://docs.google.com/spreadsheets/d/" + "<<Google Sheet ID>>" + "/export?";
var options = 'exportFormat=pdf&format=pdf' // export format
+ '&size=A4' // paper size
+ '&portrait=true' // orientation
var range = sheet.getDataRange().getA1Notation();
var response = UrlFetchApp.fetch(url + options + '&gid=' + sheet.getSheetId() + "&range=" + range,
{
headers: {
'Authorization': 'Bearer ' + token
},
muteHttpExceptions:true
});
var blob = DriveApp.createFile(response.getBlob().setName(name));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.