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.
Key room number(s) (multiple choice)
Also, create a single-choice question for the status of the keys.
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.
Conclusion
Hope this helps with managing lab operations or similar setups.