【问题标题】:How to compress picture at google sheet to convert to PDF如何在google sheet上压缩图片以转换为PDF
【发布时间】:2022-12-30 02:39:52
【问题描述】:
var changedFlag = false;
var TEMPLATESHEET='Boom-Report';

function emailSpreadsheetAsPDF() {


  DocumentApp.getActiveDocument();
  DriveApp.getFiles();

  // This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
  // Add the link to your spreadsheet here 
  // or you can just replace the text in the link between "d/" and "/edit"
  // In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI
  const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/edit");

  // We are going to get the email address from the cell "B7" from the "Invoice" sheet
  // Change the reference of the cell or the name of the sheet if it is different
  const value = ss.getSheetByName("Source Email-Boom").getRange("X3").getValue();
  const email = value.toString();

  // Subject of the email message
  const subject = ss.getSheetByName("Source Email-Boom").getRange("B3").getValue();

    // Email Text. You can add HTML code here - see ctrlq.org/html-mail
  const body = "Boom Lifts Inspection Report - Sent via Auto Generate PDI Report from Glideapps";

  // Again, the URL to your spreadsheet but now with "/export" at the end
  // Change it to the link of your spreadsheet, but leave the "/export"
  const url = 'https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/export?';

  const exportOptions =
    'exportFormat=pdf&format=pdf' + // export as pdf
    '&size=A4' + // paper size letter / You can use A4 or legal
    '&portrait=true' + // orientation portal, use false for landscape
    '&fitw=true' + // fit to page width false, to get the actual size
    '&sheetnames=false&printtitle=false' + // hide optional headers and footers
    '&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
    '&fzr=false' + // do not repeat row headers (frozen rows) on each page
    '&gid=671631174'; // the sheet's Id. Change it to your sheet ID.
    // You can find the sheet ID in the link bar. 
  // Select the sheet that you want to print and check the link,
  // the gid number of the sheet is on the end of your link.
  
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // Generate the PDF file
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // Send the PDF file as an attachement 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: ss.getSheetByName("Source Email-Boom").getRange("B3").getValue().toString() +".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });

  // Save the PDF to Drive. (in the folder) The name of the PDF is going to be the name of the Company (cell B5)
  const nameFile = ss.getSheetByName("Source Email-Boom").getRange("B3").getValue().toString() +".pdf"
  const folderID = "1ZKWq9jWmeEQlxncuTPHssCFXC3Fidmxn";
  DriveApp.getFolderById(folderID).createFile(response).setName(nameFile);
}


function on_sheet_change(event) {
  var sheetname = event.source.getActiveSheet().getName();
  var sheet = event.source.getActiveSheet();
  
  if (sheetname == 'Boom-Report') {
    emailSpreadsheetAsPDF() ;
  } else return;
}

我有触发我的编码将谷歌表格转换为 PDF 并立即发送电子邮件。 但是PDF只打开了一半的图像。

我相信这会导致我的图像尺寸非常大, 我的谷歌表看起来像这样。

但是在运行 Pdf 的 appscript 之后,会出现这样的图片。

我在图像单元格中使用的公式是

=arrayformula(image('Source Email-Boom'!CX3,1))

我相信这是因为图像的尺寸非常大。 我不确定如何压缩。你们有什么想法吗?

【问题讨论】:

  • 关于I believe it cause the size of the image was very huge.,请问图片尺寸是多少?对了,the size of the image你保存的是文件大小还是图片大小?我为我糟糕的英语水平道歉。
  • 这回答了你的问题了吗? Why do we use SpreadsheetApp.flush();?
  • 您找到解决方案了吗?为了不发生这种情况,以 kB 为单位的理想图像大小是多少?
  • @nCr78 好吧,我先压缩了图像。我正在使用“Cloudinary”压缩并链接到谷歌表格。

标签: pdf google-apps-script google-sheets


【解决方案1】:

图像问题的 tl;dr 版本:

图像通过上传时可以拥有的像素总数谷歌应用程序脚本或者Google 表格 v4 API是 1,048,576 像素。

--

对您的问题发表评论的第一个用户 (Tanaike) 已经写了很多关于这方面的内容。我偶然发现了这个gist,它比我的简短版本更好地解释了它。由于该链接将来可能无法使用或内容可能会被删除,因此我将在此稍微解释一下:

您可以上传任何您想要的图像大小(以像素为单位)的宽 x 高组合,无论文件大小(以字节为单位)如何,只要它在上述像素总数(1,048,576 像素)以内即可。这意味着您可以:

  • 1024 x 1024 像素
  • 2048 x 512 像素
  • 4096 x 256 像素

但是你不能有的例子:

  • 1025 x 1025 像素
  • 1024 x 1025 像素
  • 1025 x 1024 像素

--

最后,对于代码部分,要调整图像大小(并压缩一点),您可以使用tanaikech's ImageApp library,在调整大小部分。按照this answer 作为指南。

希望这对我有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多