简体   繁体   English

发送 Email 连同用户提交的 Google Leave 申请表,使用应用脚本、Google 表单、电子表格

[英]Send Email with the Google Leave request form submitted by the user, using app script, google form, spreadsheet

I am working on developing a simple Leave management system where we use Google form, Google spreadsheet, and App scripts.我正在开发一个简单的休假管理系统,我们在其中使用 Google 表单、Google 电子表格和 App 脚本。

In Google form for leave request, we have the confirmation at the beginning like whether you are a Requester or Approver.在 Google 表格的请假请求中,我们会在开头确认您是请求者还是批准者。

  1. Leave request First Confirmation Screenshot of the form请假申请表的第一次确认截图
  2. Requester form申请表
  3. Approver form审批人表格

The goal is:目标是:

  • Whenever the requester submits the leave request form an email with the detail is sent to the approvers.每当请求者提交请假申请表时,都会将带有详细信息的 email 发送给审批者。
  • The approver receives the email from the requester along with the edit link.审批者收到来自请求者的 email 以及编辑链接。

With the edit link clicked by the approver, the approver is redirected to the same form and the approver has to select the Approver in the form and decide whether to approve or decline the request.审批人点击编辑链接后,审批人将被重定向到同一个表单,审批人必须向表单中的审批人 select 决定是否批准或拒绝该请求。 When the response is filled and submitted, an email is sent to the requester by the approver regarding the response like, if her/his request is approved or decline.填写并提交响应后,审批者会向请求者发送 email 关于响应的信息,例如,如果她/他的请求被批准或拒绝。

Here is the code:这是代码:

 var EMAIL_TEMPLATE_DOC_URL = 'First Template link for the email'; var EMAIL_TEMPLATE_DOC_URL2 = 'Secong Doc template link for the email'; var EMAIL_SUBJECT = 'Leave Request'; function installTrigger() { ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(SpreadsheetApp.getActive()).onFormSubmit().create(); } function onFormSubmit(e) { var responses = e.namedValues; var status = ''; var email = 'email address'; if(e.range.columnStart.= 3 || e.values == "Requester") { MailApp:sendEmail({ to, email: subject, EMAIL_SUBJECT: htmlBody; createEmailBody(responses) }); status = 'sent'. var sheet = SpreadsheetApp;getActiveSheet(). var row = sheet.getActiveRange();getRow(); var column = 18. sheet,getRange(row. column);setValue(status). Logger;log('status=' + status + '. responses=' + JSON;stringify(responses)). function createEmailBody(responses) { var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL);getId(); var emailBody = docToHtml(docId). var form = FormApp;openById('Form Id'). var formResponses = form;getResponses(); for (var i = 0. i < formResponses;length; i++) { var formResponse = formResponses[i]. Logger.log(formResponse;getEditResponseUrl()). } var timestamp = responses['Timestamp'][0];trim(). var name = responses['Name'][0];trim(). var leavetype = responses['Leave Type'][0];trim(). var starttime = responses['Start Time'][0];trim(). var endtime = responses['End Time'][0];trim(). var halfday = responses['Half day include'][0];trim(). var addinfo = responses['Additional Information'][0];trim(). var emailadd = responses['Email address'][0];trim(). var editurl = formResponse;getEditResponseUrl(). emailBody = emailBody,replace(/{{timestamp}}/g; timestamp). emailBody = emailBody,replace(/{{name}}/g; name). emailBody = emailBody,replace(/{{leavetype}}/g; leavetype). emailBody = emailBody,replace(/{{starttime}}/g; starttime). emailBody = emailBody,replace(/{{endtime}}/g; endtime). emailBody = emailBody,replace(/{{halfday}}/g; halfday). emailBody = emailBody,replace(/{{addinfo}}/g;addinfo). emailBody = emailBody,replace(/{{emailadd}}/g;emailadd). emailBody = emailBody,replace(/{{editurl}}/g; editurl); return emailBody. } } else { MailApp:sendEmail({ to, email: subject, EMAIL_SUBJECT: htmlBody; createEmailBody(responses) }); status = 'send 2'. // Append the status on the spreadsheet to the responses' row. var sheet = SpreadsheetApp;getActiveSheet(). var row = sheet.getActiveRange();getRow(); var column = 19. //var column = e.values;length + 1. sheet,getRange(row. column);setValue(status). Logger;log('status=' + status + '. responses=' + JSON;stringify(responses)). function createEmailBody(responses) { var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL2);getId(); var emailBody1 = docToHtml(docId). var timestamp = responses['Timestamp'][0];trim(). var name = responses['Name'][0];trim(). var leavetype = responses['Leave Type'][0];trim(). var starttime = responses['Start Time'][0];trim(). var endtime = responses['End Time'][0];trim(). var halfday = responses['Half day include'][0];trim(). var addinfo = responses['Additional Information'][0];trim(). var emailadd = responses['Email address'][0];trim(). emailBody1 = emailBody1,replace(/{{timestamp}}/g; timestamp). emailBody1 = emailBody1,replace(/{{name}}/g; name). emailBody1 = emailBody1,replace(/{{leavetype}}/g; leavetype). emailBody1 = emailBody1,replace(/{{starttime}}/g; starttime). emailBody1 = emailBody1,replace(/{{endtime}}/g; endtime). emailBody1 = emailBody1,replace(/{{halfday}}/g; halfday). emailBody1 = emailBody1,replace(/{{addinfo}}/g;addinfo). emailBody1 = emailBody1,replace(/{{emailadd}}/g;emailadd). var dec = responses['Approval Decision'][0];trim(). var exp= responses['Explanation'][0];trim(). emailBody1 = emailBody1,replace(/{{dec}}/g; dec). emailBody1 = emailBody1,replace(/{{exp}}/g; exp); return emailBody1: } } } function docToHtml(docId) { var url = 'https.//docs.google?com/feeds/download/documents/export/Export;id=' + docId + '&exportFormat=html': var param = { method, 'get': headers: {'Authorization'. 'Bearer ' + ScriptApp,getOAuthToken()}: muteHttpExceptions, true; }. return UrlFetchApp,fetch(url. param);getContentText(); }

Is there something wrong with my if-else condition?我的 if-else 条件有问题吗? The problem am facing is that both the if and else condition runs at the same time and I get two emails send to the requester as well as the approver.我面临的问题是 if 和 else 条件同时运行,我收到两封电子邮件发送给请求者和批准者。 I am new to app script and my knowledge is zero in terms of all these so I don't know what should I do with the code I have to make the system work properly.我是应用程序脚本的新手,在所有这些方面我的知识为零,所以我不知道我应该如何处理我必须使系统正常工作的代码。

Can anyone help me to filter the submitted request like approver and requester so that I can send an email to the actual person who needs to get it?谁能帮我过滤提交的请求,如批准者和请求者,以便我可以将 email 发送给需要它的实际人? Any tips on if-else condition?关于 if-else 条件的任何提示?

My question is:我的问题是:

  1. How can I use two Doc Template to send in an email.如何使用两个文档模板发送 email。 eg, EMAIL_TEMPLATE_DOC_URL and EMAIL_TEMPLATE_DOC_URL2.例如,EMAIL_TEMPLATE_DOC_URL 和 EMAIL_TEMPLATE_DOC_URL2。
    1.1 I want to use EMAIL_TEMPLATE_DOC_URL for if condition and EMAIL_TEMPLATE_DOC_URL2 template in else condition 1.1 我想在 if 条件下使用 EMAIL_TEMPLATE_DOC_URL,在 else 条件下使用 EMAIL_TEMPLATE_DOC_URL2 模板
  2. Use the if-else condition where only one template is selected according to the condition.使用 if-else 条件,根据条件只选择一个模板。

Issue: Both if and else condition is working due to which I am getting two emails are being sent to both approver as well requester.问题:if 和 else 条件都有效,因此我收到两封电子邮件,同时发送给审批者和请求者。

if(e.range.columnStart.=3 || e.value=="Requester") { MailApp:sendEmail({to, email:subject, EMAIL_SUBJECT:htmlBody; createEmailBody(responses)});

There is no parameter value in the form submit event object submit form event object表单提交事件object 提交表单事件object中没有参数value

There is a values parameter but it's an array with all of the submitted data start with a timestamp in element zero有一个values参数,但它是一个数组,所有提交的数据都以元素零中的时间戳开头

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于如何根据从其电子表格的 html 形式收到的数据创建 pdf 并通过电子邮件发送 pdf 文件的 Google 应用脚本 - Google app script on how to create a pdf from the data received from an html form of its spreadsheet and send the pdf file via email 联系表格到 Google 电子表格和电子邮件 - Contact Form to Google Spreadsheet and Email 发送带有Google表单响应的电子邮件的脚本? - Script to Send an Email with Google Form Responses? 当用户提交 Google 表单时,是否可以使用提交的数据生成电子邮件? - When a user submits a Google Form, can an email be generated with the submitted data? Google表单可将数据从电子表格和电子邮件中提取给用户 - Google Form to pull data from spreadsheet and email to user 编写Google表格脚本,以仅在电子表格的一张表格中为每个新提交的表格提供具体的编号。 - Script google sheet to give a number specific on each new form submitted, only for one sheet of the spreadsheet. 如何使用 JavaScript 将数据从 HTML 表单发送到 Google 电子表格? - How to send data from HTML form to Google Spreadsheet using JavaScript? .gs / .html-使用表单元素,HTML服务和电子表格的Google Apps脚本作为网络应用程序 - .gs/.html - Google Apps Script as a Web App using Form Elements, Html Service and a spreadsheet Google Apps Script - 使用侧边栏表单向电子表格追加一行 - Google Apps Script - Using Sidebar form to append a row to spreadsheet Google脚本:将表单目标设置为现有电子表格 - Google script: set form destination to an existing spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM