简体   繁体   中英

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.

In Google form for leave request, we have the confirmation at the beginning like whether you are a Requester or Approver.

  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.
  • The approver receives the email from the requester along with the edit link.

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. 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.

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? 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. 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? Any tips on if-else condition?

My question is:

  1. How can I use two Doc Template to send in an email. eg, EMAIL_TEMPLATE_DOC_URL and 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
  2. Use the if-else condition where only one template is selected according to the condition.

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(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

There is a values parameter but it's an array with all of the submitted data start with a timestamp in element zero

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM