GCP/Apps Script

Apps Script๋กœ Google ์„œ๋น„์Šค ๊ธฐ๋ฐ˜์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ์•ฝ ์‹œ์Šคํ…œ ๋งŒ๋“ค๊ธฐ

whistory 2023. 4. 27. 10:28
๋ฐ˜์‘ํ˜•

 

๐Ÿ’ก Apps Script๋ฅผ ์ด์šฉํ•ด ๋ฐฐํฌํ•œ Webapp์—์„œ ์ž…๋ ฅ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ
     Google Sheets์— ์ €์žฅํ•˜๊ณ , Google Calendar ์ผ์ • ๋“ฑ๋กํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์•ฝ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณธ๋‹ค.

 

Sequence

  1. webapp ์œผ๋กœ ๋ฐฐํฌํ•œ html ํŽ˜์ด์ง€์—์„œ ๊ฐœ์ธ์ •๋ณด์™€ ์˜ˆ์•ฝ์ผ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
  2. ์ž…๋ ฅํ•œ ๋‚ ์งœ์— ์ผ์ •์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  3. Google Sheets์— ์ด๋ฆ„๊ณผ ๋‚ ์งœ๋ฅผ ์ด๋ ฅ ๊ด€๋ฆฌ ์šฉ๋„๋กœ ์ž…๋ ฅํ•œ๋‹ค.
  4. Google Calendar ์— ์ผ์ •์„ ๋“ฑ๋กํ•œ๋‹ค.
  5. ์˜ˆ์•ฝ ์™„๋ฃŒ ์ด๋ฉ”์ผ์„ ์ „์†กํ•œ๋‹ค

 

 

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());
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•