【问题标题】:Email Body and Subject and PDF File Name based on Cell Value and Specific Name基于单元格值和特定名称的电子邮件正文和主题以及 PDF 文件名
【发布时间】:2021-12-06 06:39:12
【问题描述】:

当用户提交谷歌表单时,我正在使用以下脚本生成 PDF。

脚本如下:

function afterFormSubmit(e) {
    const info = e.namedValues; 
    const pdfFile = createPDF(info);
    const entryRow = e.range.getRow();
    const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final Responses");
    ws.getRange(entryRow, 64).setValue(pdfFile.getUrl());
    ws.getRange(entryRow, 65).setValue(pdfFile.getName());
    
    sendEmail(e.namedValues ['Email address'][0],pdfFile);
}


function sendEmail(email,pdfFile){
  
  GmailApp.sendEmail(email, "Daily Transation Summary Report", "Sir, Please find the attached Daily Transaction Report for the outlet + Name of Outlet for ['Date of Report']", {
    attachments: [pdfFile],
    name: 'KCCO - Automation Team'
});

}  
  
function createPDF(info) {
  
  const pdfFolder = DriveApp.getFolderById("1ME3EqHOZzBZDBLNPHQxLjf5tWiQpmE8u");
  const tempFolder = DriveApp.getFolderById("1ebglel6vwMGXByeG4wHBBvJu9u0zJeMJ");
  const templateDoc = DriveApp.getFileById("1kyFimRMdHQZV85F5P6JyCHv1DQ3KJFv1hx5o7lIcNZo");
  
  const newTempFile = templateDoc.makeCopy(tempFolder);
  
  const openDoc = DocumentApp.openById(newTempFile.getId());
    const body = openDoc.getBody();
  body.replaceText("{Name of Person}", info['Name of Person'][0]);
  body.replaceText("{Date of Report}", info['Date of Report'][0]);
  body.replaceText("{Name of Outlet}", info['Name of Outlet'][0]);
  body.replaceText("{Opening Petty Cash Balance}", info['Opening Petty Cash Balance'][0]);
  body.replaceText("{Total Petty Cash Received}", info['Total Petty Cash Received'][0]);
  body.replaceText("{Closing Petty Cash Balance}", info['Closing Petty Cash Balance'][0]);
  body.replaceText("{Total Petty Cash Paid}", info['Total Petty Cash Paid'][0]);
  body.replaceText("{Available Float Cash Balance}", info['Available Float Cash Balance'][0]);
  body.replaceText("{Milk for Outlet}", info['Milk for Outlet'][0]);
  body.replaceText("{Cold Drink And Ice Cube Purchased}", info['Cold Drink And Ice Cube Purchased'][0]);
  body.replaceText("{Goods Purchased for Outlet}", info['Goods Purchased for Outlet'][0]);
  body.replaceText("{Staff Milk/Toast etc.}", info['Staff Milk/Toast etc.'][0]);
  body.replaceText("{Water Tank Charges}", info['Water Tank Charges'][0]);
  body.replaceText("{Repair And Maintenance}", info['Repair And Maintenance'][0]);
  body.replaceText("{Petrol Expenses}", info['Petrol Expenses'][0]);
  body.replaceText("{Freight Paid}", info['Freight Paid'][0]);
  body.replaceText("{Purchase of Soya Chaap}", info['Purchase of Soya Chaap'][0]);
  body.replaceText("{Purchase of Rumali Roti}", info['Purchase of Rumali Roti'][0]);
  body.replaceText("{Purchase of Egg Tray}", info['Purchase of Egg Tray'][0]);
  body.replaceText("{Other Expenses}", info['Other Expenses'][0]);
  body.replaceText("{Please Specify Other Expenses}", info['Please Specify Other Expenses'][0]);
  body.replaceText("{Total Tip Amount PAYTM & CARD}", info['Total Tip Amount PAYTM & CARD'][0]);
  body.replaceText("{Cash Sales - Outlet}", info['Cash Sales - Outlet'][0]);
  body.replaceText("{Cash Sales - KCCO App}", info['Cash Sales - KCCO App'][0]);
  body.replaceText("{Card Sales}", info['Card Sales'][0]);
  body.replaceText("{Credit/Udhar Sales}", info['Credit/Udhar Sales'][0]);
  body.replaceText("{Paytm Sales}", info['Paytm Sales'][0]);
  body.replaceText("{KCCO App Sales Razor Pay}", info['KCCO App Sales Razor Pay'][0]);
  body.replaceText("{Swiggy Sales}", info['Swiggy Sales'][0]);
  body.replaceText("{Zomato Sales}", info['Zomato Sales'][0]);
  body.replaceText("{Dunzo Sales}", info['Dunzo Sales'][0]);
  body.replaceText("{Zomato Gold Sales}", info['Zomato Gold Sales'][0]);
  body.replaceText("{Dine Out Sales}", info['Dine Out Sales'][0]);
  body.replaceText("{Total Cancelled Bill Amount}", info['Total Cancelled Bill Amount'][0]);
  body.replaceText("{Total Sales of the Day - Total of Above}", info['Total Sales of the Day - Total of Above'][0]);
  body.replaceText("{Total Sales of the Day - As Per PetPooja}", info['Total Sales of the Day - As Per PetPooja'][0]);
  body.replaceText("{Old Due Receipts  - Cash}", info['Old Due Receipts  - Cash'][0]);
  body.replaceText("{Old Due Receipts - Other Modes}", info['Old Due Receipts - Other Modes'][0]);
  body.replaceText("{Tip Receipts - Card}", info['Tip Receipts - Card'][0]);
  body.replaceText("{Tip Receipts - Paytm}", info['Tip Receipts - Paytm'][0]);
  body.replaceText("{Currency Note of INR 2000}", info['Currency Note of INR 2000'][0]);
  body.replaceText("{Currency Note of INR 500}", info['Currency Note of INR 500'][0]);
  body.replaceText("{Currency Note of INR 200}", info['Currency Note of INR 200'][0]);
  body.replaceText("{Currency Note of INR 100}", info['Currency Note of INR 100'][0]);
  body.replaceText("{Currency Note of INR 50}", info['Currency Note of INR 50'][0]);
  body.replaceText("{Currency Note of INR 20}", info['Currency Note of INR 20'][0]);
  body.replaceText("{Currency Note of INR 10}", info['Currency Note of INR 10'][0]);
  body.replaceText("{Other Currency Note/Coins}", info['Other Currency Note/Coins'][0]);
  body.replaceText("{Total Available Cash to Handover}", info['Total Available Cash to Handover'][0]);
  body.replaceText("{Total No. of Credit Bills}", info['Total No. of Credit Bills'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Credit Approval By// Bill No// Total Amount of Credit}", info['S.No.//Name of Person// Mobile No// Credit Approval By// Bill No// Total Amount of Credit'][0]);
  body.replaceText("{Total No. of Bills of Credit Recovery}", info['Total No. of Bills of Credit Recovery'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Mode of Payment// Bill No// Total Amount Received}", info['S.No.//Name of Person// Mobile No// Mode of Payment// Bill No// Total Amount Received'][0]);
  body.replaceText("{Total No. of Complementary Bills  During the Day}", info['Total No. of Complementary Bills  During the Day'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No//Reason// Bill No//Amount Settled}", info['S.No.//Name of Person// Mobile No//Reason// Bill No//Amount Settled'][0]);
  body.replaceText("{Total No. of Cancelled Bills During the Day}", info['Total No. of Cancelled Bills During the Day'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Bill No// Bill Amount}", info['S.No.//Name of Person// Mobile No// Bill No// Bill Amount'][0]);
  body.replaceText("{S.No.// Bill No// Mode of Payment//Tip Amount Received}", info['S.No.// Bill No// Mode of Payment//Tip Amount Received'][0]);
  body.replaceText("{No. of Cylinders Received at Outlet}", info['No. of Cylinders Received at Outlet'][0]); 
  body.replaceText("{Timestamp}", info['Timestamp'][0]); 
  body.replaceText("{Email address}", info['Email address'][0]); 
  //body.replaceText("{Total of Cash Denomination}", info['Total of Cash Denomination'][0]); 
  
  openDoc.saveAndClose();
  
  const blobPDF = newTempFile.getAs(MimeType.PDF);
  const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Name of Outlet'][0]+ " " + info['Date of Report'][0]);
  tempFolder.removeFile(newTempFile);
    
  return pdfFile;
  
}                                         

我想根据三个要求向用户发送电子邮件:

  1. 邮件的主题 - 我希望邮件的主题为“每日交易报告 - 分店名称 - 报告日期...当用户提交分店报告时,应分别更改名称和报告日期并且每次。

  2. 正文 - 每当用户提交表单时,正文还应包含出口名称和报告日期

  3. PDF 的名称应该是 DTR - 出口名称 - 报告日期...一旦用户提交表格,我可以带上出口名称和报告日期...但具体的信件我不能带。

请指导。

【问题讨论】:

  • 这听起来有点像任务列表,而不是特定的编程问题。您实际上已经在代码中拥有了执行此操作所需的所有技术,即 replaceText()sendEmail() 在扩展对象中带有 attachments 参数。查看您的正文/主题/PDF 名称的原生 JS String.replace() 方法。如果您有时间,how to ask a good question 文章也很适合阅读。

标签: javascript google-sheets script


【解决方案1】:
function afterFormSubmit(e) {
    const info = e.namedValues; 
    const pdfFile = createPDF(info);
    const entryRow = e.range.getRow();
    const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final Responses");
    ws.getRange(entryRow, 64).setValue(pdfFile.getUrl());
    ws.getRange(entryRow, 65).setValue(pdfFile.getName());
    
    sendEmail(e.namedValues ['Email address'][0],pdfFile);
}


function sendEmail(email,pdfFile){
  var DOR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final Responses").getActiveCell();
  var NOO = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final Responses").getActiveCell();
  var subject = "DTR - KCCO - " + NOO + " - " + DOR;
  var body = "Sir," +  String.fromCharCode(10)+ String.fromCharCode(10) +  "Please find the attached Daily Transaction Report for the outlet " + NOO + " for " + DOR + "." + String.fromCharCode(10) + String.fromCharCode(10)+ "--" + String.fromCharCode(10) + "Thanks & Regards" + String.fromCharCode(10) + "KCCO - Automation Team" ;
  GmailApp.sendEmail(email, subject, body, {
    attachments: [pdfFile],
    name: 'KCCO - Automation Team'
});

}  
  
function createPDF(info) {
  
  const pdfFolder = DriveApp.getFolderById("1ME3EqHOZzBZDBLNPHQxLjf5tWiQpmE8u");
  const tempFolder = DriveApp.getFolderById("1ebglel6vwMGXByeG4wHBBvJu9u0zJeMJ");
  const templateDoc = DriveApp.getFileById("1kyFimRMdHQZV85F5P6JyCHv1DQ3KJFv1hx5o7lIcNZo");
  
  const newTempFile = templateDoc.makeCopy(tempFolder);
  
  const openDoc = DocumentApp.openById(newTempFile.getId());
    const body = openDoc.getBody();
  body.replaceText("{Name of Person}", info['Name of Person'][0]);
  body.replaceText("{Date of Report}", info['Date of Report'][0]);
  body.replaceText("{Name of Outlet}", info['Name of Outlet'][0]);
  body.replaceText("{Opening Petty Cash Balance}", info['Opening Petty Cash Balance'][0]);
  body.replaceText("{Total Petty Cash Received}", info['Total Petty Cash Received'][0]);
  body.replaceText("{Closing Petty Cash Balance}", info['Closing Petty Cash Balance'][0]);
  body.replaceText("{Total Petty Cash Paid}", info['Total Petty Cash Paid'][0]);
  body.replaceText("{Available Float Cash Balance}", info['Available Float Cash Balance'][0]);
  body.replaceText("{Milk for Outlet}", info['Milk for Outlet'][0]);
  body.replaceText("{Cold Drink And Ice Cube Purchased}", info['Cold Drink And Ice Cube Purchased'][0]);
  body.replaceText("{Goods Purchased for Outlet}", info['Goods Purchased for Outlet'][0]);
  body.replaceText("{Staff Milk/Toast etc.}", info['Staff Milk/Toast etc.'][0]);
  body.replaceText("{Water Tank Charges}", info['Water Tank Charges'][0]);
  body.replaceText("{Repair And Maintenance}", info['Repair And Maintenance'][0]);
  body.replaceText("{Petrol Expenses}", info['Petrol Expenses'][0]);
  body.replaceText("{Freight Paid}", info['Freight Paid'][0]);
  body.replaceText("{Purchase of Soya Chaap}", info['Purchase of Soya Chaap'][0]);
  body.replaceText("{Purchase of Rumali Roti}", info['Purchase of Rumali Roti'][0]);
  body.replaceText("{Purchase of Egg Tray}", info['Purchase of Egg Tray'][0]);
  body.replaceText("{Other Expenses}", info['Other Expenses'][0]);
  body.replaceText("{Please Specify Other Expenses}", info['Please Specify Other Expenses'][0]);
  body.replaceText("{Total Tip Amount PAYTM & CARD}", info['Total Tip Amount PAYTM & CARD'][0]);
  body.replaceText("{Cash Sales - Outlet}", info['Cash Sales - Outlet'][0]);
  body.replaceText("{Cash Sales - KCCO App}", info['Cash Sales - KCCO App'][0]);
  body.replaceText("{Card Sales}", info['Card Sales'][0]);
  body.replaceText("{Credit/Udhar Sales}", info['Credit/Udhar Sales'][0]);
  body.replaceText("{Paytm Sales}", info['Paytm Sales'][0]);
  body.replaceText("{KCCO App Sales Razor Pay}", info['KCCO App Sales Razor Pay'][0]);
  body.replaceText("{Swiggy Sales}", info['Swiggy Sales'][0]);
  body.replaceText("{Zomato Sales}", info['Zomato Sales'][0]);
  body.replaceText("{Dunzo Sales}", info['Dunzo Sales'][0]);
  body.replaceText("{Zomato Gold Sales}", info['Zomato Gold Sales'][0]);
  body.replaceText("{Dine Out Sales}", info['Dine Out Sales'][0]);
  body.replaceText("{Total Cancelled Bill Amount}", info['Total Cancelled Bill Amount'][0]);
  body.replaceText("{Total Sales of the Day - Total of Above}", info['Total Sales of the Day - Total of Above'][0]);
  body.replaceText("{Total Sales of the Day - As Per PetPooja}", info['Total Sales of the Day - As Per PetPooja'][0]);
  body.replaceText("{Old Due Receipts  - Cash}", info['Old Due Receipts  - Cash'][0]);
  body.replaceText("{Old Due Receipts - Other Modes}", info['Old Due Receipts - Other Modes'][0]);
  body.replaceText("{Tip Receipts - Card}", info['Tip Receipts - Card'][0]);
  body.replaceText("{Tip Receipts - Paytm}", info['Tip Receipts - Paytm'][0]);
  body.replaceText("{Currency Note of INR 2000}", info['Currency Note of INR 2000'][0]);
  body.replaceText("{Currency Note of INR 500}", info['Currency Note of INR 500'][0]);
  body.replaceText("{Currency Note of INR 200}", info['Currency Note of INR 200'][0]);
  body.replaceText("{Currency Note of INR 100}", info['Currency Note of INR 100'][0]);
  body.replaceText("{Currency Note of INR 50}", info['Currency Note of INR 50'][0]);
  body.replaceText("{Currency Note of INR 20}", info['Currency Note of INR 20'][0]);
  body.replaceText("{Currency Note of INR 10}", info['Currency Note of INR 10'][0]);
  body.replaceText("{Other Currency Note/Coins}", info['Other Currency Note/Coins'][0]);
  body.replaceText("{Total Available Cash to Handover}", info['Total Available Cash to Handover'][0]);
  body.replaceText("{Total No. of Credit Bills}", info['Total No. of Credit Bills'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Credit Approval By// Bill No// Total Amount of Credit}", info['S.No.//Name of Person// Mobile No// Credit Approval By// Bill No// Total Amount of Credit'][0]);
  body.replaceText("{Total No. of Bills of Credit Recovery}", info['Total No. of Bills of Credit Recovery'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Mode of Payment// Bill No// Total Amount Received}", info['S.No.//Name of Person// Mobile No// Mode of Payment// Bill No// Total Amount Received'][0]);
  body.replaceText("{Total No. of Complementary Bills  During the Day}", info['Total No. of Complementary Bills  During the Day'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No//Reason// Bill No//Amount Settled}", info['S.No.//Name of Person// Mobile No//Reason// Bill No//Amount Settled'][0]);
  body.replaceText("{Total No. of Cancelled Bills During the Day}", info['Total No. of Cancelled Bills During the Day'][0]);
  body.replaceText("{S.No.//Name of Person// Mobile No// Bill No// Bill Amount}", info['S.No.//Name of Person// Mobile No// Bill No// Bill Amount'][0]);
  body.replaceText("{S.No.// Bill No// Mode of Payment//Tip Amount Received}", info['S.No.// Bill No// Mode of Payment//Tip Amount Received'][0]);
  body.replaceText("{No. of Cylinders Received at Outlet}", info['No. of Cylinders Received at Outlet'][0]); 
  body.replaceText("{Timestamp}", info['Timestamp'][0]); 
  body.replaceText("{Email address}", info['Email address'][0]); 
  //body.replaceText("{Total of Cash Denomination}", info['Total of Cash Denomination'][0]); 
  
  openDoc.saveAndClose();
  
  const blobPDF = newTempFile.getAs(MimeType.PDF);
  const pdfFile = pdfFolder.createFile(blobPDF).setName('DTR - KCCO - '+info['Name of Outlet'][0]+ " - " + info['Date of Report'][0]);
  tempFolder.removeFile(newTempFile);
    
  return pdfFile;
  
}                                         

【讨论】:

  • 请解释你做了什么以及它如何解决原始发帖人的问题。
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2018-08-02
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多