Last active
December 27, 2024 14:52
-
-
Save Gianguyen1234/fe2d770da70891932fd3c9f468cb534c to your computer and use it in GitHub Desktop.
Class Hiring Form appscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createClassHiringForm() { | |
// Create a new form | |
var form = FormApp.create('English Class Hiring Form'); | |
// Add 'Full Name' (Short Answer) | |
form.addTextItem().setTitle('Full Name').setRequired(true); | |
// Add 'Email Address' (Short Answer with Email Validation) | |
var emailItem = form.addTextItem().setTitle('Email Address').setRequired(true); | |
// Create the email validation | |
var emailValidation = FormApp.createTextValidation() | |
.requireTextIsEmail() // This requires the answer to be a valid email | |
.setHelpText('Please enter a valid email address.') | |
.build(); // build() is necessary to complete the validation | |
// Apply the email validation to the email field | |
emailItem.setValidation(emailValidation); | |
// Add 'Preferred Class Schedule' (Multiple Choice) | |
form.addMultipleChoiceItem() | |
.setTitle('Preferred Class Schedule') | |
.setChoiceValues(['Morning', 'Afternoon', 'Evening']) | |
.setRequired(true); | |
// Add 'Level of English' (Multiple Choice) | |
form.addMultipleChoiceItem() | |
.setTitle('Level of English') | |
.setChoiceValues(['Beginner', 'Intermediate', 'Advanced']) | |
.setRequired(true); | |
// Add 'Message / Additional Information' (Paragraph) | |
form.addParagraphTextItem().setTitle('Message / Additional Information').setHelpText('Tell us more about your learning goals.'); | |
// Add 'Agreement to Terms' (Checkbox) | |
form.addCheckboxItem() | |
.setTitle('I agree to the terms and conditions.') | |
.setChoiceValues(['I agree']) | |
.setRequired(true); | |
// Create a new Google Spreadsheet to store responses | |
var spreadsheet = SpreadsheetApp.create('English Class Hiring Responses'); | |
// Link the form to the created Google Spreadsheet | |
form.setDestination(FormApp.DestinationType.SPREADSHEET, spreadsheet.getId()); | |
// Log the URL of the form for easy access | |
Logger.log('Form created! You can access it at: ' + form.getEditUrl()); | |
Logger.log('Responses will be stored in the spreadsheet: ' + spreadsheet.getUrl()); | |
// Create a trigger to send emails after form submission | |
ScriptApp.newTrigger('sendEmails') | |
.forForm(form) | |
.onFormSubmit() | |
.create(); | |
} | |
function sendEmails() { | |
// The ID of the spreadsheet | |
var spreadsheetId = 'your-spreadsheet-id'; | |
// Open the spreadsheet by its ID | |
var spreadsheet = SpreadsheetApp.openById(spreadsheetId); | |
var sheet = spreadsheet.getSheets()[0]; | |
// Get the data from the sheet | |
var data = sheet.getDataRange().getValues(); | |
// Loop through each row in the spreadsheet (skipping the header row) | |
for (var i = 1; i < data.length; i++) { | |
var row = data[i]; | |
// Extract form response data based on the actual columns | |
var fullName = row[1]; // Full Name (2nd column, index 1) | |
var email = row[2]; // Email Address (3rd column, index 2) | |
var preferredSchedule = row[3]; // Preferred Class Schedule (4th column, index 3) | |
var englishLevel = row[4]; // Level of English (5th column, index 4) | |
var message = row[5]; // Message / Additional Information (6th column, index 5) | |
// Declare time based on preferred schedule | |
var scheduleTime = ''; | |
switch (preferredSchedule) { | |
case 'Morning': | |
scheduleTime = '9:00 AM - 12:00 PM'; | |
break; | |
case 'Afternoon': | |
scheduleTime = '1:00 PM - 4:00 PM'; | |
break; | |
case 'Evening': | |
scheduleTime = '6:00 PM - 8:00 PM'; | |
break; | |
default: | |
scheduleTime = 'Not specified'; | |
} | |
// HTML Email content with CSS styles | |
var subject = "Your English Class Hiring Information"; | |
var htmlBody = ` | |
<html> | |
<head> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
color: #333; | |
line-height: 1.6; | |
background-color: #f4f4f4; | |
margin: 0; | |
padding: 20px; | |
} | |
.container { | |
background-color: #ffffff; | |
padding: 20px; | |
border-radius: 10px; | |
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); | |
max-width: 600px; | |
margin: 0 auto; | |
} | |
h1 { | |
color: #4CAF50; | |
text-align: center; | |
} | |
p { | |
font-size: 16px; | |
color: #555; | |
margin: 10px 0; | |
} | |
.highlight { | |
font-weight: bold; | |
color: #4CAF50; | |
} | |
.footer { | |
text-align: center; | |
font-size: 12px; | |
color: #888; | |
margin-top: 20px; | |
} | |
.footer a { | |
color: #4CAF50; | |
text-decoration: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>English Class Hiring Information</h1> | |
<p>Dear <span class="highlight">${fullName}</span>,</p> | |
<p>Thank you for your interest in our English classes. Here are your schedule:</p> | |
<p><span class="highlight">Preferred Class Schedule:</span> ${preferredSchedule}</p> | |
<p><span class="highlight">Class Time:</span> ${scheduleTime}</p> | |
<p><span class="highlight">Level of English:</span> ${englishLevel}</p> | |
<p><span class="highlight">Message / Additional Information:</span><br>${message}</p> | |
<p>Please make sure to be on time for your first class so we can get started without delay. We’re looking forward to your participation!</p> | |
<div class="footer"> | |
<p>Best regards,<br>The English Class Team</p> | |
<p><a href="https://example.com">Visit our website</a></p> | |
</div> | |
</div> | |
</body> | |
</html> | |
`; | |
// Send the email with both text and HTML bodies | |
MailApp.sendEmail({ | |
to: email, | |
subject: subject, | |
body: "This is a plain text fallback email. Please enable HTML to view the rich content.", | |
htmlBody: htmlBody | |
}); | |
Logger.log("Email sent to: " + email); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment