简体   繁体   English

在 Apps 脚本中将 Google 表格转换为 PDF 格式时文件大小增加

[英]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?此问题的可能原因是什么以及如何解决?

Your spreadsheet might have many empty cells which get converted into the pdf the way you do it.您的电子表格可能有许多空单元格,它们会按照您的方式转换为 pdf。

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.

相关问题 使用谷歌应用程序脚本将html文件转换为adobe pdf时,文件大小会增加 - File size increases when converting an html file to adobe pdf using google apps script 使用 Google 应用脚本在 Google 表格中上传 PDF 文件 - Upload PDF file in Google sheet using Google apps script Google UI Apps脚本:将电子表格转换为PDF时空白的PDF - Google UI Apps Script: Blank PDF when converting spreadsheet to PDF 通过 Apps 脚本将工作表导出为 PDF(现在某些更改不再可搜索且文件大小更大) - Export Sheet via Apps Script as PDF (something change now no more searchable and much bigger file size) 使用 Google Apps 脚本中的自定义页眉或页脚将 Google 表格导出为 PDF - Exporting Google Sheet as PDF with Custom Headers or Footers in Google Apps Script 通过Google-Apps-Script将Goole文档转换为pdf时,如何消除多余的多余页面? - How to get rid of unwanted extra pages when converting a goole document to pdf via google-apps-script? Google Apps脚本创建的文件不是PDF - Google Apps script created file is not PDF 谷歌表格应用程序脚本,如何格式化保存到谷歌驱动器的 pdf? - Google sheets apps script, how to format a pdf saved to google drive? 使用 Google Apps 脚本从 Google 表格导出 PDF 时的 setName - setName when exporting a PDF from Google Sheets with Google Apps Script Google Apps Script:如何将(只有一个)Google Sheet 以 PDF 格式保存到 Drive 文件夹? - Google Apps Script: How to save (Just one) Google Sheet to Drive Folder as PDF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM