จัดการคำขออุปกรณ์ของพนักงานใหม่

ระดับการเขียนโค้ด: มือใหม่
ระยะเวลา: 10 นาที
ประเภทโปรเจ็กต์: การทำงานอัตโนมัติที่มี ทริกเกอร์ที่ขับเคลื่อนด้วยเหตุการณ์ และทริกเกอร์ที่ขึ้นกับเวลา

วัตถุประสงค์

  • ทำความเข้าใจโซลูชันที่ใช้
  • ทำความเข้าใจหน้าที่ของบริการ Apps Script ในโซลูชัน
  • ตั้งค่าสคริปต์
  • เรียกใช้สคริปต์

เกี่ยวกับโซลูชันนี้

พนักงานใหม่มักจะต้องใช้การเข้าถึงระบบและอุปกรณ์จากฝ่ายไอที หากต้องการจัดการคำขอเหล่านี้ คุณสามารถสร้างแบบฟอร์มด้วย Google ฟอร์มเพื่อให้ผู้ใช้ระบุสิทธิ์เข้าถึงและอุปกรณ์ที่พนักงานต้องการ เมื่อฝ่ายไอทีดำเนินการตามคำขอและอัปเดตสถานะแล้ว ผู้ส่งคำขอจะได้รับการแจ้งเตือนทางอีเมล

ภาพหน้าจอของแบบฟอร์มคำขออุปกรณ์

วิธีการทำงาน

สคริปต์สร้างแบบฟอร์มคำขออุปกรณ์ คุณปรับแต่งรายการบนแบบฟอร์มได้ในโค้ดของสคริปต์ตัวอย่าง เมื่อมีผู้ส่งแบบฟอร์ม สคริปต์จะส่งการแจ้งเตือนทางอีเมลไปยังผู้ติดต่อที่กำหนดสำหรับคำขอต่างๆ เมื่อสถานะคำขอในสเปรดชีตเปลี่ยนเป็น "เสร็จสมบูรณ์" สคริปต์จะส่งอีเมลยืนยันไปยังผู้ที่ส่งแบบฟอร์ม

บริการ Apps Script

โซลูชันนี้ใช้บริการต่อไปนี้

  • บริการฟอร์ม สร้างฟอร์มสำหรับคำขอฝ่ายไอที
  • บริการสเปรดชีต - ตรวจสอบเพื่อดูว่ามีแบบฟอร์มคำขออยู่แล้วหรือไม่เพื่อลดปัญหาการทำซ้ำ จัดการการตอบกลับของแบบฟอร์มโดยย้ายไปยังชีตรอดำเนินการและเสร็จสมบูรณ์ตามต้องการ
  • บริการอีเมล - สร้างและส่งอีเมล คำขอและอีเมลแจ้งเตือนการเสร็จสมบูรณ์
  • บริการสคริปต์ - สร้างทริกเกอร์ การทำงานครั้งแรกจะเริ่มทำงานเมื่อมีการส่งแบบฟอร์ม และอีกแท็กหนึ่งเริ่มทำงานทุก 5 นาทีเพื่อตรวจสอบว่ามีการทำเครื่องหมายสถานะของคำขอเป็น "เสร็จสมบูรณ์" หรือไม่

ข้อกำหนดเบื้องต้น

หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้

  • บัญชี Google (บัญชี Google Workspace อาจต้องได้รับการอนุมัติจากผู้ดูแลระบบ)
  • เว็บเบราว์เซอร์ที่มีการเชื่อมต่ออินเทอร์เน็ต

ตั้งค่าสคริปต์

สร้างโปรเจ็กต์ Apps Script

  1. คลิกที่ปุ่มต่อไปนี้เพื่อทำสำเนาสเปรดชีตจัดการคำขออุปกรณ์ของพนักงาน โครงการ Apps Script สำหรับโซลูชันนี้จะแนบมากับสเปรดชีต
    ทำสำเนา
  2. คลิกส่วนขยาย > Apps Script
  3. ถัดจากตัวแปร REQUEST_NOTIFICATION_EMAIL ให้แทนที่อีเมลตัวอย่างด้วยอีเมลของคุณ
  4. คลิกบันทึก ไอคอนบันทึก

ตั้งค่าสเปรดชีต

  1. กลับไปที่สเปรดชีตและคลิกคำขออุปกรณ์ > ตั้งค่า คุณอาจต้องรีเฟรชหน้าเพื่อให้เมนูที่กำหนดเองนี้ปรากฏขึ้น
  2. เมื่อได้รับข้อความแจ้ง ให้ให้สิทธิ์สคริปต์ หากหน้าจอความยินยอมของ OAuth แสดงคำเตือน แอปนี้ไม่ได้รับการยืนยัน ให้ดำเนินการต่อโดยเลือกขั้นสูง > ไปที่ {Project Name} (ไม่ปลอดภัย)

  3. คลิกคำขออุปกรณ์ > ตั้งค่าอีกครั้ง

เรียกใช้สคริปต์

  1. คลิกเครื่องมือ > จัดการแบบฟอร์ม > ไปที่แบบฟอร์มที่ใช้งานอยู่
  2. กรอกข้อมูลและส่งแบบฟอร์ม
  3. ตรวจสอบอีเมลเพื่อดูการแจ้งเตือนเกี่ยวกับคำขออุปกรณ์
  4. กลับไปที่สเปรดชีตและในชีตคำขอที่รอดำเนินการ ให้เปลี่ยนสถานะของคำขอเป็นเสร็จสมบูรณ์
  5. สคริปต์จะส่งอีเมลอีกฉบับภายใน 5 นาทีเพื่อแจ้งให้ทราบว่าคำขอเสร็จสมบูรณ์แล้ว สคริปต์จะย้ายคำขอจากชีตคำขอที่รอดำเนินการไปยังชีตคำขอที่เสร็จสมบูรณ์

ตรวจสอบโค้ด

หากต้องการตรวจสอบโค้ด Apps Script สำหรับโซลูชันนี้ ให้คลิกดูซอร์สโค้ดด้านล่าง

ดูซอร์สโค้ด

Code.gs

solutions/automations/equipment-requests/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/equipment-requests

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Update this variable with the email address you want to send equipment requests to.
const REQUEST_NOTIFICATION_EMAIL = 'request_intake@example.com';

// Update the following variables with your own equipment options.
const AVAILABLE_LAPTOPS = [
  '15" high Performance Laptop (OS X)',
  '15" high Performance Laptop (Windows)',
  '15" high performance Laptop (Linux)',
  '13" lightweight laptop (Windows)',
];
const AVAILABLE_DESKTOPS = [
  'Standard workstation (Windows)',
  'Standard workstation (Linux)',
  'High performance workstation (Windows)',
  'High performance workstation (Linux)',
  'Mac Pro (OS X)',
];
const AVAILABLE_MONITORS = [
  'Single 27"',
  'Single 32"',
  'Dual 24"',
];

// Form field titles, used for creating the form and as keys when handling
// responses.
/**
 * Adds a custom menu to the spreadsheet.
 */
function onOpen() {
  SpreadsheetApp.getUi().createMenu('Equipment requests')
      .addItem('Set up', 'setup_')
      .addItem('Clean up', 'cleanup_')
      .addToUi();
}

/**
 * Creates the form and triggers for the workflow.
 */
function setup_() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  if (ss.getFormUrl()) {
    let msg = 'Form already exists. Unlink the form and try again.';
    SpreadsheetApp.getUi().alert(msg);
    return;
  }
  let form = FormApp.create('Equipment Requests')
      .setCollectEmail(true)
      .setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId())
      .setLimitOneResponsePerUser(false);
  form.addTextItem().setTitle('Employee name').setRequired(true);
  form.addTextItem().setTitle('Desk location').setRequired(true);
  form.addDateItem().setTitle('Due date').setRequired(true);
  form.addListItem().setTitle('Laptop').setChoiceValues(AVAILABLE_LAPTOPS);
  form.addListItem().setTitle('Desktop').setChoiceValues(AVAILABLE_DESKTOPS);
  form.addListItem().setTitle('Monitor').setChoiceValues(AVAILABLE_MONITORS);

  // Hide the raw form responses.
  ss.getSheets().forEach(function(sheet) {
    if (sheet.getFormUrl() == ss.getFormUrl()) {
      sheet.hideSheet();
    }
  });
  // Start workflow on each form submit
  ScriptApp.newTrigger('onFormSubmit_')
      .forForm(form)
      .onFormSubmit()
      .create();
  // Archive completed items every 5m.
  ScriptApp.newTrigger('processCompletedItems_')
      .timeBased()
      .everyMinutes(5)
      .create();
}

/**
 * Cleans up the project (stop triggers, form submission, etc.)
 */
function cleanup_() {
  let formUrl = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
  if (!formUrl) {
    return;
  }
  ScriptApp.getProjectTriggers().forEach(function(trigger) {
    ScriptApp.deleteTrigger(trigger);
  });
  FormApp.openByUrl(formUrl)
      .deleteAllResponses()
      .setAcceptingResponses(false);
}

/**
 * Handles new form submissions to trigger the workflow.
 *
 * @param {Object} event - Form submit event
 */
function onFormSubmit_(event) {
  let response = mapResponse_(event.response);
  sendNewEquipmentRequestEmail_(response);
  let equipmentDetails = Utilities.formatString('%s\n%s\n%s',
      response['Laptop'],
      response['Desktop'],
      response['Monitor']);
  let row = ['New',
    '',
    response['Due date'],
    response['Employee name'],
    response['Desk location'],
    equipmentDetails,
    response['email']];
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName('Pending requests');
  sheet.appendRow(row);
}

/**
 * Sweeps completed and cancelled requests, notifying the requestors and archiving them
 * to the completed sheet.
 *
 * @param {Object} event
 */
function processCompletedItems_() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let pending = ss.getSheetByName('Pending requests');
  let completed = ss.getSheetByName('Completed requests');
  let rows = pending.getDataRange().getValues();
    for (let i = rows.length; i >= 2; i--) {
      let row = rows[i -1];
      let status = row[0];
      if (status === 'Completed' || status == 'Cancelled') {
          pending.deleteRow(i);
          completed.appendRow(row);
          console.log("Deleted row: " + i);
          sendEquipmentRequestCompletedEmail_({
            'Employee name': row[3],
            'Desk location': row[4],
            'email': row[6],
          });
        }
      };
}

/**
 * Sends an email notification that a new equipment request has been submitted.
 *
 * @param {Object} request - Request details
 */
function sendNewEquipmentRequestEmail_(request) {
  let template = HtmlService.createTemplateFromFile('new-equipment-request.html');
  template.request = request;
  template.sheetUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
  let msg = template.evaluate();
  MailApp.sendEmail({
    to: REQUEST_NOTIFICATION_EMAIL,
    subject: 'New equipment request',
    htmlBody: msg.getContent(),
  });
}

/**
 * Sends an email notifying the requestor that the request is complete.
 *
 * @param {Object} request - Request details
 */
function sendEquipmentRequestCompletedEmail_(request) {
  let template = HtmlService.createTemplateFromFile('request-complete.html');
  template.request = request;
  let msg = template.evaluate();
  MailApp.sendEmail({
    to: request.email,
    subject: 'Equipment request completed',
    htmlBody: msg.getContent(),
  });
}

/**
 * Converts a form response to an object keyed by the item titles. Allows easier
 * access to response values.
 *
 * @param {FormResponse} response
 * @return {Object} Form values keyed by question title
 */
function mapResponse_(response) {
  let initialValue = {
    email: response.getRespondentEmail(),
    timestamp: response.getTimestamp(),
  };
  return response.getItemResponses().reduce(function(obj, itemResponse) {
    let key = itemResponse.getItem().getTitle();
    obj[key] = itemResponse.getResponse();
    return obj;
  }, initialValue);
}

new-equipment-request.html

solutions/automations/equipment-requests/new-equipment-request.html
<!DOCTYPE html>
<!--
 Copyright 2022 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<html>
  <body>
    <p>
    A new equipment request has been made by <?= request.email ?>.
    </p>

    <p>
    Employee name: <?= request['Employee name'] ?><br/>
    Desk location name: <?= request['Desk location'] ?><br/>
    Due date: <?= request['Due date'] ?><br/>
    Laptop model: <?= request['Laptop'] ?><br/>
    Desktop model: <?= request['Desktop'] ?><br/>
    Monitor(s): <?= request['Monitor'] ?><br/>
    </p>

    See <a href="<?= sheetUrl ?>">the spreadsheet</a> to take or assign this item.
  </body>
</html>

request-complete.html

solutions/automations/equipment-requests/request-complete.html
<!DOCTYPE html>
<!--
 Copyright 2022 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<html>
  <body>
    <p>
    An equipment request has been completed.
    </p>

    <p>
    Employee name: <?= request['Employee name'] ?><br/>
    Desk location name: <?= request['Desk location'] ?><br/>
    </p>
  </body>
</html>

ผู้ร่วมให้ข้อมูล

ตัวอย่างนี้ดูแลโดย Google ด้วยความช่วยเหลือจากผู้เชี่ยวชาญด้านนักพัฒนาซอฟต์แวร์ของ Google

ขั้นตอนถัดไป