[英]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 表格的请假请求中,我们会在开头确认您是请求者还是批准者。
The goal is:目标是:
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:我的问题是:
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.