๋ฐ์ํ
๐ก Apps Script๋ฅผ ์ด์ฉํด ๋ฐฐํฌํ Webapp์์ ์ ๋ ฅ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ
Google Sheets์ ์ ์ฅํ๊ณ , Google Calendar ์ผ์ ๋ฑ๋กํ๋ ๊ฐ๋จํ ์์ฝ ์์คํ ์ ๋ง๋ค์ด๋ณธ๋ค.
Sequence
- webapp ์ผ๋ก ๋ฐฐํฌํ html ํ์ด์ง์์ ๊ฐ์ธ์ ๋ณด์ ์์ฝ์ผ์๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค.
- ์ ๋ ฅํ ๋ ์ง์ ์ผ์ ์ด ๋ฑ๋ก๋์ด ์๋์ง ํ์ธํ๋ค.
- Google Sheets์ ์ด๋ฆ๊ณผ ๋ ์ง๋ฅผ ์ด๋ ฅ ๊ด๋ฆฌ ์ฉ๋๋ก ์ ๋ ฅํ๋ค.
- Google Calendar ์ ์ผ์ ์ ๋ฑ๋กํ๋ค.
- ์์ฝ ์๋ฃ ์ด๋ฉ์ผ์ ์ ์กํ๋ค
1. ์ฝ๋ ์์ฑ
1.1 code.gs
function doGet(e) {
Logger.log(JSON.stringify(e));
var htmlOutput = HtmlService.createTemplateFromFile('page.html');
const sheetId = "google_sheet_id";
const sheetName = "calender ์ฐ๋";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
if ( !e.parameter['username'] || !e.parameter['input_date'] || !e.parameter['phone'] || !e.parameter['email'] ) {
htmlOutput.message = '์
๋ ฅํ ๊ฐ์ด ์์ต๋๋ค.';
} else {
htmlOutput.message = `${e.parameter['username']} ๋ ${e.parameter['input_date']} ์ผ์ ์์ฝ์ด ์ ์๋์์ต๋๋ค.`;
// Google Sheets์ ์ด๋ ฅ ๋ฑ๋ก
sheet.getRange(sheet.getLastRow()+1, 1, 1, 4).setValues([[e.parameter['username'], e.parameter['phone'], e.parameter['email'], e.parameter['input_date']]])
.setBorder(true, true, true, true, true, true);
// Google Calendar์ ์ผ์ ๋ฑ๋ก
setCalendar(e.parameter['username'], e.parameter['input_date']);
// ์๋ฃ๋ฉ์ผ ์ ์ก
sendEmail(e.parameter['username'], e.parameter['email'], e.parameter['input_date']);
}
htmlOutput.url = getUrl();
return htmlOutput.evaluate();
}
function getUrl() {
const url = ScriptApp.getService().getUrl();
return url;
}
/**
* Google Calendar Event ๋ฑ๋ก
* @param {string} name = ์ด๋ฆ
* @param {string} date = ๋ ์ง
*/
function setCalendar(name, date) {
CalendarApp.createAllDayEvent(`${name}๋ ์์ฝ`, new Date(date));
}
/**
* Send Email.
* ์ด๋ฉ์ผ์ ์ด์ฉํ Notify.
* @param {string} name = ๋ฐ๋์ฌ๋ ์ด๋ฆ
* @param {string} recipient = ๋ฐ๋์ฌ๋ ์ด๋ฉ
* @param {string} date = ์์ฝ์ผ
*/
function sendEmail(name, recipient, date) {
try {
const subject = `${name}๋ ${date} ์์ฝ ์๋ฃ ๋ฉ์ผ`;
let html = `<h1>${name} ๋.</h1>`;
html += `<div>${date} ์์ฝ์ด ์ ์์ ์ผ๋ก ์ฒ๋ฆฌ๋์์ต๋๋ค.</div>`;
html += `<div>์ด์ฉํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.</div>`;
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: html
});
return {"result": "200", "message" : "Success!"};
} catch (error) {
Logger.log(error.message);
return {"result": "999", "message" : error.message};
}
}
1.2 page.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
</head>
<body>
<form action="<?= url ?>" method="GET">
<div class="container">
<div class="row frame">
<h5 class="mt-4 text-center">Apps Script ์น์ฑ ์์ฝ ์์คํ
</h5>
<h6 class="mb-4 text-center">์์ฝ์ ๋ณด ์
๋ ฅ Form</h6>
<!-- create form element here -->
<div class="form-group mb-4 box">
์ฑํจ : <input type="text" class="form-control inp mb-3" id="username" name="username" placeholder="์ฑํจ์ ์
๋ ฅํด์ฃผ์ธ์." autocomplete="off">
</div>
<div class="form-group mb-4 box">
์ ํ๋ฒํธ : <input type="text" class="form-control inp mb-3" id="phone" name="phone" placeholder="์ ํ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." autocomplete="off">
</div>
<div class="form-group mb-4 box">
์ด๋ฉ์ผ : <input type="text" class="form-control inp mb-3" id="email" name="email" placeholder="์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์." autocomplete="off">
</div>
<div class="form-group mb-4 box">
์์ฝ๋ ์ง : <input type="date" class="form-control inp mb-3" id="input_date" name="input_date" placeholder="์
๋ ฅ" autocomplete="off">
</div>
<!-- create form until element here -->
<sapn><?= message ?></sapn>
<div class="form-group mt-4 mb-4 text-center">
<input type="submit" class="btn btn-info" name="Submit" /><br>
</div>
</div>
</div>
</form>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
</body>
</html>
1.3 ์น ์คํ ํ๋ฉด
1.4 Google Sheets ์ด๋ ฅ ๊ด๋ฆฌ์ฉ ํ ํ๋ฆฟ
2. ๊ธฐ๋ฅ ์คํ
2.1 ํ๋ฉด์์ ์คํ
๊ฐ์ธ์ ๋ณด์ ์์ฝ๋ ์ง๋ฅผ ์ ๋ ฅํ๊ณ ์ ์ถ์ ๋๋ฅธ๋ค.
2.2 Google Sheets์ ํ ์ถ๊ฐ
Google Sheets์์๋ [2.1] ํ๋ฉด์์ ์ ๋ ฅํ ์ ๋ณด๋ค์ ํ์ด ์ถ๊ฐ ๋ ๊ฑธ ๋ณผ ์ ์๋ค.
2.3 Google Calendar์ ์ผ์ ์ถ๊ฐ
Google Calendar์์๋ [2.1] ํ๋ฉด์์ ์ ๋ ฅํ ์ผ์ ์ด ๋ฑ๋ก๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
2.4 ์๋ฃ ๋ฉ์ผ ์ ์ก
[2.1] ํ๋ฉด์์ ์ ๋ ฅํ ์ด๋ฉ์ผ์ฃผ์๋ก ์๋ฆผ๋ฉ์ผ์ด ๊ฐ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
3. ์ค๋ณต ์ฒ๋ฆฌ
์ฌ๋ฌ ์ฌ๋์ด ๋์ผํ ์ผ์ ์ ์์ฝ์ ํ๋ฉด ์์ฝ์ด ์ค๋ณต๋ ๊ฒ์ด๋ค.
์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ํด๋น ๋ ์ง์ ๋ฑ๋ก๋ ์ผ์ ์ ์๋ฅผ ํ์ธํ๋ค.
function checkSchedule() {
var checkDate = new Date("2023-04-26");
var cnt = CalendarApp.getDefaultCalendar().getEventsForDay(checkDate);
console.log(`${checkDate}์ผ ์ ์ผ์ ๊ฐฏ์๋ ${cnt.length}`);
checkDate = new Date("2023-04-28");
cnt = CalendarApp.getDefaultCalendar().getEventsForDay(checkDate);
console.log(`${checkDate}์ผ ์ ์ผ์ ๊ฐฏ์๋ ${cnt.length}`);
}
์ด์ ์ผ์ ์ด ์กด์ฌํ๋ฉด return ํ๋ ๋ถ๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ค.
function doGet(e) {
Logger.log(JSON.stringify(e));
var htmlOutput = HtmlService.createTemplateFromFile('page.html');
const sheetId = "google_sheet_id";
const sheetName = "calender ์ฐ๋";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
if ( !e.parameter['username'] || !e.parameter['input_date'] || !e.parameter['phone'] || !e.parameter['email'] ) {
htmlOutput.message = '์
๋ ฅํ ๊ฐ์ด ์์ต๋๋ค.';
} else {
const existCnt = CalendarApp.getDefaultCalendar().getEventsForDay(new Date(e.parameter['input_date']));
// ํด๋น๋ ์ง์ ์ค๋ณต์ผ์ ์ฌ๋ถ ํ์ธ
if ( existCnt < 1 ) {
htmlOutput.message = `${e.parameter['username']} ๋ ${e.parameter['input_date']} ์ผ์ ์์ฝ์ด ์ ์๋์์ต๋๋ค.`;
// Google Sheets์ ์ด๋ ฅ ๋ฑ๋ก
sheet.getRange(sheet.getLastRow()+1, 1, 1, 4).setValues([[e.parameter['username'], e.parameter['phone'], e.parameter['email'], e.parameter['input_date']]])
.setBorder(true, true, true, true, true, true);
// Google Calendar์ ์ผ์ ๋ฑ๋ก
setCalendar(e.parameter['username'], e.parameter['input_date']);
// ์๋ฃ๋ฉ์ผ ์ ์ก
sendEmail(e.parameter['username'], e.parameter['email'], e.parameter['input_date']);
} else {
htmlOutput.message = `${e.parameter['input_date']} ์ผ์ ์ด๋ฏธ ์์ฝ๋์ด ์๋ ์ผ์ ์
๋๋ค.`;
}
}
htmlOutput.url = getUrl();
return htmlOutput.evaluate();
}
4. ์ผ์ ๋ฑ๋ก ๋ฐฉ๋ฒ
์ผ์ ๋ฑ๋ก ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ค.
์ข ์ผ ์ผ์ ๋ฑ๋ก๊ณผ,
์๊ฐ๋ ์ผ์ ์ ๋ฑ๋ก.
/**
* Google Calendar Event ๋ฑ๋ก
* @param {string} name = ์ด๋ฆ
* @param {string} date = ๋ ์ง
*/
function setCalendar(name, date) {
const event = CalendarApp.createEvent(`${name}๋ ์์ฝ`, new Date(`${date} 10:00:00`), new Date(`${date} 18:00:00`));
console.log(event.getId());
const eventAllDay = CalendarApp.createAllDayEvent(`${name}๋ ์์ฝ`, new Date(date));
console.log(eventAllDay.getId());
}
๋ฐ์ํ