延迟 Appscript 3 秒
Delay the Appscript for 3 seconds
我正在为输入的每一行使用触发器,它将从 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 指出的方法。但我意识到这不是主要问题。我的 google sheet 看起来像这样。
但是在 appscript 运行 之后 Pdf 会出现这样的图片。
我在图像单元格中使用的公式是
=arrayformula(image('Source Email-Boom'!CX3,1))
我相信这是因为图像的尺寸非常大。
我不确定如何压缩。你们有什么想法吗?
正如 Cooper 指出的那样,尝试 Utilities.sleep()
该参数以毫秒为单位表示,因此对于 3 秒的延迟,您可以将其添加到需要暂停的脚本中:
Utilities.sleep(3000)
我正在为输入的每一行使用触发器,它将从 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 指出的方法。但我意识到这不是主要问题。我的 google sheet 看起来像这样。
但是在 appscript 运行 之后 Pdf 会出现这样的图片。
我在图像单元格中使用的公式是
=arrayformula(image('Source Email-Boom'!CX3,1))
我相信这是因为图像的尺寸非常大。 我不确定如何压缩。你们有什么想法吗?
正如 Cooper 指出的那样,尝试 Utilities.sleep()
该参数以毫秒为单位表示,因此对于 3 秒的延迟,您可以将其添加到需要暂停的脚本中:
Utilities.sleep(3000)