Final App Script, Google Sheet no longer being used

This commit is contained in:
Norm Rasmussen
2022-03-31 09:57:04 -04:00
parent b7ddc861c5
commit 983b07b14c

View File

@ -1,129 +1,219 @@
/* /*
Custom Fields: This script sends a daily reminder to the Sales and Marketing channel as to which
Channel = customfield_10121 lines of data are unclean and not filled out. Currently, it is only pulling from the last 5 days.
Campaign = customfield_10120 This will be sent out between 3-4pm so that AEs can get their data in and clean before EOD.
SDR = customfield_10122 Any questions or changes needed, see Norm.
Start Date = customfield_10015 Webhook URL in this sheet is for #sales-n-marketing channel.
Users:
Travis Nardin = 5d9cb42c0265ca0db955b965
Dan Peski = 603318815ddf020069969cad
Norm Rasmussen = 6092af20d353800068863d15
Michael Valido = 61fbf60cd8d7cf006a90941c
Jon Newfield = 6092af212c2f6c0068ec92c4
Isabel Katz = 620145241fec260068c107e5
Charles McGovern = 5eaaf1c4021ae30ba8fcb184
Nick Zuppe = 60eddda64257a90070aeebef
Doug Goldsmith = 61294a4845f753006951a590
Nick Appleby = 6183eab7892c420072f9c437
Quba Williams-Wilfong = 62101fe4e41f76006a6f6510
Without accounts: Adan, Drew
*/ */
// Setup of the sheet
const sheet = SpreadsheetApp.getActiveSheet();
function myFunction() { // Setup of the date range to compare. Currently, it is using 5 days until Present.
var URL = "https://northpass.atlassian.net/rest/api/3/issue/"; var now = new Date();
// var accountID = "6092af20d353800068863d15"; var formatNow = Utilities.formatDate(now, 'America/New_York', 'MM/dd/yyyy'); // Today
var token = "2NrKYv22TLWnxTo7EhU3633E"; var daysToSubtract = 2;
// var UserCredentials = Utilities.base64Encode(accountID + ":" + token); var withinWeek = new Date(now.getTime()-daysToSubtract*(3600*24*1000));
var user = "nrasmussen@northpass.com"; var formatWeek = Utilities.formatDate(withinWeek, 'America/New_York', 'MM/dd/yyyy'); // 5 Days ago
// var pass = "c9QK\>4^fxiHt!";
var UserCredentials = Utilities.base64Encode(user + ":" + token);
// Setting up data range and empty arrays // Counter for formatting the final list
var list = []; var meetingCount = 0;
const sheet = SpreadsheetApp.getActiveSheet();
var startRow = 244; // First col of data to process
var numRows = 450; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn()).getValues();
// var data = dataRange.filter(function(r){
// return r.join("").length>0;
// });
for (col in dataRange) { // For a data col within the entire data range // Other empty Globals
var col = dataRange[col]; var finalAEList;
let sdr = col[0]; // Column - A var tagUsers;
let name = col[1]; // Column - B
let rawDate = Utilities.formatDate(col[3], 'America/New_York', 'yyyy-MM-dd'); /*
let bookingDate = Utilities.formatDate(col[2], 'America/New_York', 'MM/dd/yyyy'); // Column - C This function will create two empty arrays, one for the list of missed entries and one for tagging users in Slack
let meetingDate = Utilities.formatDate(col[3], 'America/New_York', 'MM/dd/yyyy'); // Column - D First, the function gets the data ranges in spreadsheed and adds those columns to an array index.
let company = col[4]; // Column - E Second, the for loop cycles through the super long array (4 results per line). Based on the if statement,
let channel = col[5]; // Column - F the loop removes all arrays that don't fit the statement.
let result = col[7]; // Column - H */
let campaign = col[8]; // Column - I function findMeetings() { // Setting up data range and empty arrays
let info = col[9]; // Column - J var startRow = 2; // First row of data to process
let list = [sdr, name, rawDate ,bookingDate, meetingDate, company, channel, result, campaign, info]; var numRows = sheet.getLastRow()-1; // Number of rows to process
list.toString(); var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
Logger.log(list); var data = dataRange.getValues();
let sdrDict = { var slackingAEListOne = [];
"Mike" : var tagList = [];
"61fbf60cd8d7cf006a90941c",
"Nick" : for (i in data) { // For a data row within the entire data range
"60eddda64257a90070aeebef", var row = data[i];
"Appleby" : let attended = row[6]; // Column G
"6183eab7892c420072f9c437", var name = row[1]; // Column - B
"Doug" : var date = Utilities.formatDate(row[3],'America/New_York','MM/dd/yyyy'); // Column - D
"61294a4845f753006951a590", var company = row[4]; // Column - E
"Quba" : let missedEntries = [name, date, company, attended];
"62101fe4e41f76006a6f6510", /*
Adding a For Loop will pull a result for EACH element, aka 4 results per line.
This pulls one for each group of missedEntries
This then removes the last value (attended, since we already know it is a blank), converts to a string,
and adds to a new array. The counter will compare if there is more than one entry in the array.
*/
if ((missedEntries[3] == "") && (missedEntries[1] >= formatWeek) && (missedEntries[1] <= formatNow)) {
meetingCount += 1;
missedEntries.pop();
missedEntries.toString();
slackingAEListOne.push(missedEntries);
//Logger.log(slackingAEListOne);
}; };
if (list.includes("Norm")) { };
var data = { /* Now outside of the if statement:
"fields": { the array is built, and we want each group to be on a new line, remove the commas and add a hyphen.
"project": { The counter counts if there is 1 or more meetings, or not. If 0, it sends a certain message, not tagging anyone.
"key": "NORMPIPE", If it is one or more, it splices by the first array (index 0), adds a new line, and replaces commans with hyphens.
}, */
"issuetype": { if (meetingCount >= 1) {
"id": "10268", var slackingAEListTwo = slackingAEListOne.splice(0).join('\n');
}, var finalAEList = slackingAEListTwo.replace(/,/g, ' - ');
"summary": list[5], // This if statement is going to only tag those who appear in the previous list.
"description": { // No need to tag people who have done their work and contribute to clean data.
"type": "doc", if (finalAEList.includes('Norm')) {
"version": 1, tagList.push('<@U020KRBDSDQ>');
"content": [ }
{ if (finalAEList.includes('Dan')) {
"type": "paragraph", tagList.push('<@U01P7DTFSQZ>');
"content": [ }
{ if (finalAEList.includes('Charles')) {
"type": "text", tagList.push('<@U01286MQUS2>');
"text": "Booked Date - " + list[3] + '\n' + }
"Meeting Date - " + list[4] + '\n' + if (finalAEList.includes('Travis')) {
"Description from Tracker: " + list[9] + '\n' + tagList.push('<@UFE3T14UX>');
"Last Status - " + list[7] }
} if (finalAEList.includes('Nick')) {
] tagList.push('<@U0276LMA70F>');
} }
], if (finalAEList.includes('Mike')) {
}, tagList.push('<@U027MAQUPM0>');
"customfield_10015": list[2], // Start (Meeting) Date - rawDate }
"customfield_10120": [ // Campaign if (finalAEList.includes('Doug')) {
list[8] tagList.push('<@U02CK55FHFX>');
], }
"customfield_10122": [{"id" : sdrDict[list[0]]}], // SDR if (finalAEList.includes('Isabel')) {
"customfield_10121": [ // Channel tagList.push('<@U03019S7R6F>');
list[6] }
], if (finalAEList.includes('Ryan')) {
tagList.push('<@U0325CWA3N3>');
}
var tagUsers = tagList.toString();
/*
Now, we're building the payload for the Slack Message.
This is very specific and prone to errors, so check that it works using Slack's tool:
https://app.slack.com/block-kit-builder/
Copy from Rich:
":rotating_light::rotating_light::rotating_light: New Meeting Tracker Alert! :rotating_light::rotating_light::rotating_light:
It's your daily reminder to update the new meeting tracker.
If you've been tagged, please address this by EOD."
*/
let payloadText =
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":rotating_light::rotating_light::rotating_light: New Meeting Tracker Alert! :rotating_light::rotating_light::rotating_light:"
} }
}, },
payload = JSON.stringify(data); {
Logger.log(payload); "type": "section",
var headers = { "text": {
"Accept": "application/json", "type": "mrkdwn",
"Content-Type": "application/json", "text": "It's your daily reminder to update the new meeting tracker."
"Authorization": "Basic " + UserCredentials, }
"muteHttpExceptions": true, },
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "If you've been tagged, please address this by EOD."
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": finalAEList // Sends list with AE, meeting date, and company
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "In case you need it, here's a link to the sheet. :point_right:"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Sales New Meeting Tracker",
"emoji": true
},
"value": "sales_tracker_link_123",
"url": "https://docs.google.com/spreadsheets/d/150nSuHQLJHpJaYdQ6KHeN7nQ6iexwtnQEpicM77PK8A/edit#gid=0 | First Meeting Tracker",
"action_id": "button-action"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": tagUsers // Tags with slack user numbers if they appear in finalSend
}
} }
var options = { ]
"method": "POST", };
"headers": headers, // This is standard operating procedure to creating the payload and destination
"payload": payload const webhook = "https://hooks.slack.com/services/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";
} const options = {
//Logger.log(options); method: "post",
var response = UrlFetchApp.fetch(URL, options); contentType: "application/json",
//Logger.log(response); muteHttpExceptions: true,
var respCode = response.getResponseCode(); payload: JSON.stringify(payloadText),
//Logger.log(respCode); };
} const sendMsg = UrlFetchApp.fetch(webhook, options);
} var respCode = sendMsg.getResponseCode();
}; //Logger.log(sendMsg); // Debug to confirm send
//Logger.log(respCode); // Debug to show errors, if any
/*
This is the else statement that sends a certain message not tagging anyone and saying that data is clean.
*/
} else {
let noMeetingMsg =
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":star::star::star: New Meeting Tracker Alert! :star::star::star:"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Good job, everyone! All meetings from the last 5 days are up to date. Keep doing what you're doing!"
}
}
]
};
// This is standard operating procedure to creating the payload and destination
const webhook = "https://hooks.slack.com/services/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";
const options = {
method: "post",
contentType: "application/json",
muteHttpExceptions: true,
payload: JSON.stringify(noMeetingMsg),
};
const sendMsg = UrlFetchApp.fetch(webhook, options);
var respCode = sendMsg.getResponseCode();
//Logger.log(sendMsg); // Debug to confirm send
//Logger.log(respCode); // Debug to show errors, if any
}
};