Key management done by sending google form answers to slack [stable version]

# Introduction
I am going to introduce a system that has been reliably in use, based on my previous post about managing keys by sending responses from a Google Form to Slack.

# Implementation
## Preparation
First, follow the instructions on this site to set up your Slack for incoming webhooks:
[My previous system](https://goworkship.com/magazine/slack-usage-3/)

## Google Form
Create a multiple-choice list for selecting keys.
![Screenshot 2024-03-28 at 22.54.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3346908/eb5e2980-8499-37fa-5cab-66827d89f865.png)
Key room number(s) (multiple choice)

Also, create a single-choice question for the status of the keys.
![Screenshot 2024-03-28 at 22.55.47.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3346908/4aea9434-ff93-b301-ea9b-8fe2cf5a84a0.png)
Status
Key Borrowed
Key returned
I closed the key (I have the key without returning it)
I unlocked the key (I went back to my room)

Do not forget to enable `Make this a required question` and `Collect email addresses`.

## Code
I have added comments where you need to make changes.
```javascript
function onFormSubmit(event) {
  var scriptProperties = PropertiesService.getScriptProperties();

  var email = event.response.getRespondentEmail();
  var name = getNameFromEmail(email);

  var message = "<!everyone> " + name + "\n";
  var timestamp = event.response.getTimestamp();
  var date = new Date(timestamp);
  var formattedDate = Utilities.formatDate(date, "JST", "yyyy/MM/dd HH:mm:ss" + "\n");
  message += "Time: " + formattedDate;
  message += "────\nCurrent Operation:\n";

  var items = event.response.getItemResponses();
  var selectedKeys = [];
  var status = "";

  for (var i = 0; i < items.length; i++) {
    var response = items[i].getResponse();
    var title = items[i].getItem().getTitle();
    if (title === "Key Room Number (multiple selections allowed)") { // Title of the Google Form question
      selectedKeys = Array.isArray(response) ? response : [response];
    } else if (title === "Status") { // Title of the Google Form question
      status = response;
    }
  }

  for (var i = 0; i < selectedKeys.length; i++) {
    scriptProperties.setProperty(selectedKeys[i], status);
    message += selectedKeys[i] + ": " + status + "\n";
  }

  message += "────\nStatus of All Keys:\n";

  var allKeys = ["101", "102", "201", "A-1"]; // Room numbers prepared in the Google Form
  var maxLength = allKeys.reduce((max, key) => Math.max(max, key.length), 0);

  for (var i = 0; i < allKeys.length; i++) {
    var keyStatus = scriptProperties.getProperty(allKeys[i]) || "Not Borrowed";
    var paddedKey = allKeys[i].padEnd(maxLength, ' ');
    message += paddedKey + ": " + keyStatus + "\n";
  }

  message += "────\n";
  message += "Form Link: " + "your_google_form_link" + "\n"; // Include link to the Google Form in the message

  UrlFetchApp.fetch(
    "your_webhook_url", // Insert Webhook URL
    {
      "method": "POST",
      "contentType": "application/json",
      "payload": JSON.stringify({ "text": message })
    }
  );
}


//// Mapping of Email Addresses to Names (Email Address: Name) ////
var emailNameMapping = {
  "aaaaaaaaaaa@qiita.ac.jp": "AAA",
  "bbbbbbbbbbb@qiita.ac.jp": "BBB",
  "ccccccccccc@qiita.ac.jp": "CCC",
};
///////////////////////////////////////////////////////////////////

// Function to get name from email address
function getNameFromEmail(email) {
  return emailNameMapping[email] || email;
}
```

## What Gets Sent to Slack
When it works, it looks something like this.
![Image 2024-03-28 at 23.03.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3346908/37ca20d9-b43c-ae0a-e1c4-fe78473ab305.jpeg)

# Conclusion
Hope this helps with managing lab operations or similar setups.