Skip to content

Instantly share code, notes, and snippets.

@Andrew-Chen-Wang
Created October 14, 2021 21:04
Show Gist options
  • Save Andrew-Chen-Wang/eb480d38b708490d6605feecc9d9352e to your computer and use it in GitHub Desktop.
Save Andrew-Chen-Wang/eb480d38b708490d6605feecc9d9352e to your computer and use it in GitHub Desktop.
Save Plate shouter for ICC Houses
let spreadsheet = "Today's Save Plates"
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function increment_char(c) {
return c !== 'Z' ? String.fromCharCode(c.charCodeAt(0) + 1) : 'A';
}
function rtrim(x, characters) {
var start = 0;
var end = x.length - 1;
while (characters.indexOf(x[end]) >= 0) {
end -= 1;
}
return x.substr(0, end + 1);
}
function increment_str(s) {
lpart = rtrim(s, 'Z');
num_replacements = s.length - lpart.length;
new_s = lpart ? lpart.slice(0, -1) + increment_char(lpart.slice(-1)) : 'A';
new_s += 'A'.repeat(num_replacements);
return new_s
}
function getNextDayLink() {
// Gets the link for the next day and sets the current link on the cell that allows people to traverse to there in the first place
// TODO YOu can get the link prefix by right clicking a cell and getting the link to the cell
// Then erase the cell at the end (for the range query parameter, but don't erase range=)
let link = "";
const ss = SpreadsheetApp.getActive().getSheetByName('Special Requests');
let cells = ss.getRange("A6:BQ6");
let i = 0;
for (let x of cells.getFontWeights()[0]) {
if (x === 'bold') break;
i++;
}
let col = 'A';
// +1 for the next day
for (let temp = 0; temp < i + 1; temp++) {
col = increment_str(col);
}
link = link + col + "6";
ss.setCurrentCell(ss.getRange("A3:A3")).setValue(link);
return link;
}
function formatName(string) {
if (!string.includes(" ")) string = string.concat(" ");
let [a,b] = string.split(" ");
a = capitalizeFirstLetter(a);
b = capitalizeFirstLetter(b);
return a + " " + b;
}
function buildAlert(data) {
let names = new Set();
for (let row of data) {
for (let name of row) {
if (!name) continue;
let formattedName = formatName(name);
if (name.toLowerCase().includes("not needed")) {
if (names.has(formattedName)) {
names.delete(formattedName);
continue;
}
}
names.add(formattedName);
}
}
names = Array.from(names);
names.sort();
let _formattedNames = "";
for (let name of names) {
_formattedNames = _formattedNames.concat("- " + name + "\n");
}
let payload = {
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":bell: *Today's Save Plates from the Spreadsheet* :bell: for " + new Date().toLocaleDateString("en-US", {timeZone: 'America/Detroit'})
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This is only from the spreadsheet. You can still message on Slack for save plates today, and they should be checked by the chefs!",
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": _formattedNames,
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Fill out your Save Plate requests for tomorrow here (automatically sets at next available cell, so just type your name): [Link](" + getNextDayLink() + ")",
}
}
]
};
return payload;
}
function sendAlert(payload) {
const webhook = ""; // TODO Paste your webhook URL here
var options = {
"method": "post",
"contentType": "application/json",
"muteHttpExceptions": true,
"payload": JSON.stringify(payload)
};
try {
UrlFetchApp.fetch(webhook, options);
} catch(e) {
Logger.log(e);
}
}
function buildReport() {
const ss = SpreadsheetApp.getActive();
let data = ss.getSheetByName(spreadsheet).getRange("A7:B100").getValues();
let payload = buildAlert(data);
sendAlert(payload);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment