如何从 Google Sheet 生成 PDF 并在编辑 sheet 中的特定单元格时将副本发送到特定的电子邮件帐户?
How to generate PDF from a Google Sheet and send copies to specific email accounts when a specific cell in the sheet is edited?
我正在尝试为一个项目开具发票。它的工作方式是一个人填写所需的详细信息,并在填写详细信息后立即填写。他们会编辑一个特定的单元格(可能是一个下拉列表,如“发票已填写”和“发票不完整”)。一旦他们点击填充,它就会将 sheet 作为 PDF 文件发送到不同的电子邮件。
我很难通过应用程序脚本解决这个问题。
这是一个示例
function createAndSendPDF() {
const docID = '___________'; // ID of spreadsheet
const feuilleID = '0'; // sheetID
const email = 'emailAddress@email.com';
// const dossier = DriveApp.getFolderById('____________'); // ID of Folder if you want to save a copy of the pdf file
const d = Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd")
const fichier = 'myFileName' + "_" + d + ".pdf"
const objet = 'Test pdf';
const corps = "Please find ...";
const url = 'https://docs.google.com/spreadsheets/d/' + docID + '/export?';
const exportOptions =
'exportFormat=pdf&format=pdf' +
'&size=A4' +
'&portrait=true' +
'&fitw=true' +
'&sheetnames=false&printtitle=false' +
'&pagenumbers=false&gridlines=false' +
'&fzr=false' +
'&gid=' + feuilleID;
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var reponse = UrlFetchApp.fetch(url + exportOptions, params).getBlob();
GmailApp.sendEmail(email, objet, corps, {
htmlBody: corps,
attachments: [{
fileName: fichier,
content: reponse.getBytes(),
mimeType: "application/pdf"
}]
});
// dossier.createFile(reponse.setName(fichier));
}
您只需添加类型为 onEdit(e)
的触发器并控制何时将复选框值设置为 true。
然后你基本上需要发送一封带有附件的邮件:
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName('Proforma Sheet')
const PDF_MIME = "application/pdf"
const listEmail = ["test1@gmail.com", "test2@gmail.com"]
const sendPDF = (e) => {
const { range: { rowStart, columnStart } } = e
const {value} = e
if (rowStart === 11 && columnStart === 12 && value === "TRUE" ) {
const copy = DriveApp.getFileById(ss.getId()).getAs(PDF_MIME).copyBlob()
listEmail.forEach(email=>{
MailApp.sendEmail(email, "Your Spreadsheet", "See attched", {
attachments: [copy]
})
})
}
}
并为 sendPDF
函数创建一个 installable edit trigger。
我正在尝试为一个项目开具发票。它的工作方式是一个人填写所需的详细信息,并在填写详细信息后立即填写。他们会编辑一个特定的单元格(可能是一个下拉列表,如“发票已填写”和“发票不完整”)。一旦他们点击填充,它就会将 sheet 作为 PDF 文件发送到不同的电子邮件。
我很难通过应用程序脚本解决这个问题。
这是一个示例
function createAndSendPDF() {
const docID = '___________'; // ID of spreadsheet
const feuilleID = '0'; // sheetID
const email = 'emailAddress@email.com';
// const dossier = DriveApp.getFolderById('____________'); // ID of Folder if you want to save a copy of the pdf file
const d = Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd")
const fichier = 'myFileName' + "_" + d + ".pdf"
const objet = 'Test pdf';
const corps = "Please find ...";
const url = 'https://docs.google.com/spreadsheets/d/' + docID + '/export?';
const exportOptions =
'exportFormat=pdf&format=pdf' +
'&size=A4' +
'&portrait=true' +
'&fitw=true' +
'&sheetnames=false&printtitle=false' +
'&pagenumbers=false&gridlines=false' +
'&fzr=false' +
'&gid=' + feuilleID;
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var reponse = UrlFetchApp.fetch(url + exportOptions, params).getBlob();
GmailApp.sendEmail(email, objet, corps, {
htmlBody: corps,
attachments: [{
fileName: fichier,
content: reponse.getBytes(),
mimeType: "application/pdf"
}]
});
// dossier.createFile(reponse.setName(fichier));
}
您只需添加类型为 onEdit(e)
的触发器并控制何时将复选框值设置为 true。
然后你基本上需要发送一封带有附件的邮件:
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName('Proforma Sheet')
const PDF_MIME = "application/pdf"
const listEmail = ["test1@gmail.com", "test2@gmail.com"]
const sendPDF = (e) => {
const { range: { rowStart, columnStart } } = e
const {value} = e
if (rowStart === 11 && columnStart === 12 && value === "TRUE" ) {
const copy = DriveApp.getFileById(ss.getId()).getAs(PDF_MIME).copyBlob()
listEmail.forEach(email=>{
MailApp.sendEmail(email, "Your Spreadsheet", "See attched", {
attachments: [copy]
})
})
}
}
并为 sendPDF
函数创建一个 installable edit trigger。