【发布时间】:2022-01-27 13:20:18
【问题描述】:
我为每一行输入一个触发器,它将从 excel 转换为 pdf 并立即发送电子邮件。
在我需要转换的 PDF 中,包含照片。为了进行转换,从触发到转换照片的时间必须是 1 秒或一半,所以照片会出现一半,我相信它还没有完成转换。
所以,我需要可以暂停运行 3 秒的脚本,然后继续执行脚本,以便完全加载照片。
var changedFlag = false;
var TEMPLATESHEET='Boom-Report';
function emailSpreadsheetAsPDF() {
//********************LIKE I NEED TO PAUSE FOR 3 SECONDS HERE*****************////
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;
}
刚才我尝试了@corren 和@que 指出的内容。但我意识到这不是主要问题。我的谷歌表格是这样的。
但是在 appscript 运行 Pdf 之后会出现这样的图片。
我在图像单元格中使用的公式是
=arrayformula(image('Source Email-Boom'!CX3,1))
我相信这是因为图像的尺寸非常大。 我不确定如何压缩。你们有什么想法吗?
【问题讨论】:
-
@Cooper 所以我必须添加 Utilities.sleep(3) - 因为我需要暂停 3 秒?
-
尝试阅读链接
-
请将您的图像压缩问题作为另一个问题发布。我们希望我们的问题得到明确定义和重点突出,以便其他人在遇到类似问题时能够找到它们。
-
这能回答你的问题吗? Why do we use SpreadsheetApp.flush();?
标签: excel pdf google-apps-script google-sheets