【发布时间】: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