0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

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

Google Form

Create a multiple-choice list for selecting keys.
Screenshot 2024-03-28 at 22.54.40.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
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.

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

Conclusion

Hope this helps with managing lab operations or similar setups.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?