Send email for each project owner including all notes and subtasks - javascript

I am trying to write an Appscript/Javascript that can match a project to its tasks and notes relationship. Grab all the tasks + notes for each project and email the owner of the project. Im not sure how to do this at all. Since they are in three diffrent sheets.
Right now I have a script to send Weekly emails for each persons tasks assigned to them that works becasue it only requires the single IPTM_Task sheet.
My spreadsheet looks like this Tasks:
Task Sheet
SubTasks:
Subtasks
Notes:
Notes
Script to email task owners
function sendEmails() {
let s = '';
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ITPM_Tasks");
const lastRow = sh.getLastRow();
const startRow = 2; // First row of data to process
const numRows = lastRow - 1; // Number of rows to process
const rg = sh.getRange(startRow, 3, numRows, 6);
const vs = rg.getValues();
let oners = {pA:[]};
vs.forEach((r,i) => {
let [name,desc,status,owner,due] = r;
if(status != 'Complete') {
if(!oners.hasOwnProperty(owner)) {
oners[owner]=[];
oners[owner].push(r);
oners.pA.push(owner)
} else {
oners[owner].push(r);
}
}
});
let subject = 'Weekly Reminder: The following tasks are assigned to you.';
oners.pA.forEach(p => {
let msg = `These Tasks below are assigned to you:\n`
oners[p].forEach((r,i) => {
let [name,desc,status,owner,due] = r;
msg += `Task - ${i+1}\n`;
msg += `Description: ${desc}\n`;
msg += `Due Date: ${due.toDateString()}\n\n`
});
msg += `some message to task owners`;
MailApp.sendEmail(oners[p][0][3], subject, msg);
});
}
EDIT:
Basically, I would like the script to see the Project ID on the Task Sheet grab the project name (Project) and the Owner
Then find the related "SubTask" Project Names" and related "Notes" to that project name
and send the Project owner an email with
Project Name
Subtask 1
subtask 2...
Note 1
Note 2
idealoutput

Makes sense to break it down, e.g.like this:
const ss = SpreadsheetApp.getActive()
const projects = ss.getSheetByName("Task Sheet").getDataRange().getValues()
projects.forEach( project => {
const projectId = project[0]
const status = project[2]
if( status.toLowerCase() == "completed") return
const notes = getNotesByProjectId(projectId)
const subtasks = getSubtasksByProjectId(projectId)
// here you can build your message
let message = ""
notes.forEach( note => {
// add conditions here
message += note[2] + "\n"
})
subtasks.forEach( subtask => {
// add conditions here
message += subtask[3] + "\n"
})
// and then send it
})
function getNotesByProjectId( projectId ){
const allNotes = ss.getSheetByName("Notes").getDataRange().getValues()
// Filter where Column 2 (index 1) is the project
const filteredNotes = allNotes.filter( note => note[1] == projectId )
return filteredNotes
}
function getSubtasksByProjectId( projectId ){
const allNotes = ss.getSheetByName("Subtasks").getDataRange().getValues()
// Filter where Column 3 (index 2) is the project
const filteredNotes = allNotes.filter( note => note[2] == projectId )
return filteredNotes
}
Reference
Sheet.getDataRange

Related

How to compare a value with each item of range

for the last 3 days (my first 3 days of coding) i've been trying to code an script to get my google contacts that have certain keyword, it being "Catamarca", on their name and also to delete that keyword after they've been added to the spreadsheet leaving only their name.
I've been succesfull in all of this. But now i want to only run the script on an existing database, and only run it if the new contacts are not on the sheet already, and not write over the existing ones.
Here is my code so far:
function impContacts() {
// variables
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GContacts");
var grupo = ContactsApp.getContactGroupById("http://www.google.com/m8/feeds/groups/email#gmail.com/base/6")
const contactos = grupo.getContacts();
const arraycontacts = [];
// get the last row on B with data on it
const lastRow = sheet.getLastRow();
const Avals = sheet.getRange("B2:B" + lastRow).getValues();
const Alast = lastRow - Avals.reverse().findIndex(c => c[0] != '');
Logger.log(Alast);
var rangeInteres = sheet.getRange(2, 1, Alast, sheet.getLastColumn()).getValues();
// look for contacts that has in their name the word "Catamarca", and save them with their id, name and phone.
contactos.forEach(contacto => {
var phone = ""
if (contacto.getPhones()[0]) {
phone = contacto.getPhones()[0].getPhoneNumber()
};
var name = ""
if (contacto.getFullName().match("Catamarca")){
name = contacto.getFullName();
};
var idcont = ""
if (name == rangeInteres.forEach(namme =>
{
oldName = namme[1];
Logger.log(oldName);}))
{
idcont =
rangeInteres.forEach(id => {
oldId= id[0];
Logger.log(oldId);})
}
else
{
idcont = contacto.getId().replace(/\D/g, '')
};
const datoscont = [idcont, name, phone];
arraycontacts.push(datoscont);
})
// save new contact
sheet.getRange(2, 1, arraycontacts.length, 3).setValues(arraycontacts);
// look for "catamarca"
range = sheet.getRange("B2:B")
var textFind = range.createTextFinder("Catamarca");
textFind.matchEntireCell(false);
textFind.ignoreDiacritics(true);
textFind.matchCase(false);
var textFound = textFind.findNext();
// Si encuentra coincidencia reemplazar por ""
if (textFound !== null) {
var vals = textFound.getValues();
textFind.replaceAllWith("");
}
}
What i need to keep the most is the ID of the existing contacts (the IDs are different from the ones coming from google contacts but the names are the same), because they are linked to an app created using AppSheet.
I believe there should be a way to accomplish this by editing this part of the code
var idcont = ""
if (name == rangeInteres.forEach(namme =>
{
oldName = namme[1];
Logger.log(oldName);}))
{
idcont =
rangeInteres.forEach(id => {
oldId= id[0];
Logger.log(oldId);})
}
else
{
idcont = contacto.getId().replace(/\D/g, '')
};
In my head and with my current knowledge, the code should be working, but it's not, I mean, it runs, but overwrites everything.

BUTTON to move data from multiple google sheets into one master

My boss told to move same set of data from 40 multiple google sheets into one master sheets which are filled by 40 people separately currently am working on it with an ADD on function
is there any way to put a submit button on each sheets so that when user fill the data and click on submit button the data go direct to the master sheets ?
I have the script which is working fine with sheets but the only cons of that is the script only combine the tabs into same spreadsheet
here is code :
const masterSheet = "ASM-A30";
const masterSheetFormulaCell = "A2";
const ignoreSheets = ["Verified NDRx","Business Tracker","NDRX","PMT_EBx","PMT_EBx.","NDRx PMT Business Tracker.","Analysis"];
const dataRange = "A2:AA";
const checkRange = "A2:A" ;
//end set variables
const ss = SpreadsheetApp.getActiveSpreadsheet() ;
ignoreSheets.push(masterSheet) ;
const allsheets = ss.getSheets() ;
const filteredListofSheets = allsheets.filter(s => ignoreSheets.indexOf(s.getSheetName()) == -1);
let formulaArray = filteredListofSheets.map(s => `FILTER({'${s.getSheetName()}'!${dataRange},"${s.getSheetName()} - Row "&ROW('${s.getSheetName()}'!${dataRange})}, '${s.getSheetName()}'!${checkRange}<>"")`);
let formulaText = "={" + formulaArray.join(";")+ "}"
//console.1og( formulaText) ;
ss. getSheetByName(masterSheet).getRange(masterSheetFormulaCell) .setFormula(formulaText) ;
}
Try this
const ignoreSheets = ["Verified NDRx","Business Tracker","NDRX","PMT_EBx","PMT_EBx.","NDRx PMT Business Tracker.","Analysis"];
const masterSheet = "ASM-A30";
function compile() {
let result = []
SpreadsheetApp.getActiveSpreadsheet().getSheets().filter(s => ignoreSheets.indexOf(s.getSheetName()) == -1).forEach((s,i) => {
let [headers, ...data] = s.getDataRange().getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
if(result.length) {
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(masterSheet)
master.clearContents()
master.getRange(1, 1, result.length, result[0].length).setValues(result)
SpreadsheetApp.getActiveSpreadsheet().toast('Updated!');
}
}
to add rows, change
if (i == 0) { }
and try
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(masterSheet)
// master.clearContents()
master.getRange(+master.getLastRow()+1, 1, result.length, result[0].length).setValues(result)
SpreadsheetApp.getActiveSpreadsheet().toast('Updated!');

Send Email to Task owners as long as task is not complete and Project is not backlogged

I have this script below, that emails the person a copy of all of their assigned tasks that are not complete.
However, I would like it to skip all Tasks where the Main Projects status is "backlog"
Here is my current code, and a link to a copy of the tables:
function sendEmails() {
// Retrieve Project IDs, Project names and Project Status
const project = sheetITPM.getRange("A2:J" + sheetITPM.getLastRow()).getValues().map(r => ({ id: r[0], name: r[1], status: r[2]}));
//get Tasks Sheet and all rows needed
let s = '';
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ITPM_Tasks");
const lastRow = sh.getLastRow();
const startRow = 2; // First row of data to process
const numRows = lastRow - 1; // Number of rows to process
const rg = sh.getRange(startRow, 3, numRows, 6);
const vs = rg.getValues();
let oners = {pA:[]};
//function to NOT include projects with status: "backlog"
//dont include tasks with status "Complete"
vs.forEach((r,i) => {
let [name,desc,status,owner,due] = r;
if(status != 'Complete') {
if(!oners.hasOwnProperty(owner)) {
oners[owner]=[];
oners[owner].push(r);
oners.pA.push(owner)
} else {
oners[owner].push(r);
}
}
});
//email subject and start message
let subject = 'IT Board - Weekly Reminder: The following tasks are assigned to you.';
oners.pA.forEach(p => {
let msg = `These Tasks below are assigned to you:\n`
//task array
oners[p].forEach((r,i) => {
let [name,desc,status,owner,due] = r;
msg += `Task - ${i+1}\n`;
msg += `Description: ${desc}\n`;
msg += `Due Date: ${due}\n\n`
});
msg += `notify the IT Project Management Team of any updates\n\nThank You`;
//send email to the task owner with their task array + subject + our message
MailApp.sendEmail(oners[p][0][3], subject, msg);
});
}
Link to sheet:
https://docs.google.com/spreadsheets/d/1aGojtDy9sDpGexiT_ixF_H_95GG_uiYeAkNUWzYkKSE/edit?usp=sharing
You can try getting the list of IDs for backlog projects first and then using includes to check if the project name of the task belongs to the list of IDs. See code below:
Script:
let oners = {pA:[]};
// get backlog projects, then create an array containing the ids of those projects
const backlogProjects = project.filter(x => x.status == 'Backlog').map(x => x.id);
// dont include tasks with status "Complete"
vs.forEach((r,i) => {
let [name,desc,status,owner,due] = r;
// check if project name matches the IDs present in the generated array above
if(status != 'Complete' && !backlogProjects.includes(name)) {
if(!oners.hasOwnProperty(owner)) {
oners[owner]=[];
oners[owner].push(r);
oners.pA.push(owner)
} else {
oners[owner].push(r);
}
}
});
Output:
Output shows no nl7k28t in the tasks as that project is still in Backlog status.

how to export all contact by API in hubspot to google spredsheet with app script

I try to export all contacts from hubspot to spreadsheet,
but I do not know how I can do it
You would be wise to use the people api since the Contact API will begin to be shut down after June 15. I've provided a function that I use.
You welcome to it.
Use what you wish. Modify as needed.
function displayCurrentContacts() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Contacts');
sh.clearContents();
const vs = [['Name', 'Emails']];
const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
//Logger.log(resp);
const data = JSON.parse(resp);
let m = 0;
let n = 0;
data.connections.forEach((ob1, i) => {
if (ob1.emailAddresses && ob1.emailAddresses.length > 0) {
let emails = [... new Set(ob1.emailAddresses.map(ob2 => ob2.value))];//used set to insure I get unique list
//let emails = ob1.emailAddresses.map(ob2 => ob2.value);
let name;
m += emails.length;
if (ob1.names && ob1.organizations) {
name = ob1.names[0].displayName + '\n' + ob1.organizations[0].name;
++n;
} else if (ob1.names) {
name = ob1.names[0].displayName;
++n;
} else if (ob1.organizations) {
name = ob1.organizations[0].name;
++n;
}
vs.push([name, emails.sort().join('\n')])
}
});
vs.push([n, m])
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs)
sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).sort({ column: 1, sortAscending: true });
}

Whitelisting a channel for a command

I am in the progress of making a discord bot. This is one of the commands that auto-does itself.
What it is supposed to do it's if someone type .team buy 1, save the data that comes out of another bot.
I would like to whitelist this command to 2 specific channels, identified by their channel id, and just ignore the message if it is not in the 2 channels.
How can I edit the code to do it?
const fs = require("fs");
module.exports.run = (client, message) => {
if ([509042284793430032, 501784044649054231].includes(message.channel.id)) return;
try {
//check if it's a different message //like when a user enters "team buy 234"
if (message.embeds[0].description.indexOf("❓") === 0) return;
//retrieve the team data
var teamData = JSON.parse(fs.readFileSync(client.config.dataFolder + "/teamUpgrades.json", "utf8"));
//get the current purchases data from the message
var arr = message.embeds[0].description.split("\n");
//loop and save the data in "items" object
for (var i = 0; i < arr.length; i++) {
if (arr[i] == "") continue;
if (arr[i].indexOf("Unlocks") > -1) continue; //skip locked items
var opt = arr[i].split("|"); //item's info
var name = opt[0].trim();
if (name.indexOf("**") > -1)
name = name.substring(name.indexOf("**") + 2, name.length - 2).trim(); //bold
else
name = name.split(" ")[1]; //not bold
var price = opt[1].trim();
price = price.substring(3, price.length - 1);
price = parseInt(price.split(",").join(""));
var count = opt[2].trim();
count = parseInt(count.substring(1, count.length - 2).split(",").join(""));
var eps = opt[3].trim();
eps = parseFloat(eps.split(" ")[0].substring(1));
//if the item doesn't exist, create it
if (!teamData.items[name]) teamData.items[name] = {};
teamData.items[name].price = price;
teamData.items[name].eps = eps;
teamData.items[name].count = count;
}
//the best item to buy, let's give it a very high number first
var minItem = {
name: "",
min: Number.MAX_SAFE_INTEGER
};
for (var name in teamData.items) {
//The average price/eps
var average = Number(teamData.items[name].price) / Number(teamData.items[name].eps);
//if the current item is less than the minimum item, replace it.
if (average < minItem.min) {
minItem.name = name;
minItem.min = average;
}
}
//write the current data into the json file
fs.writeFileSync(client.config.dataFolder + "/teamUpgrades.json", JSON.stringify(teamData));
message.channel.send(minItem.name);
} catch (err) {
console.log(err);
}
}
You can check if message.channel.id is equal to one of your IDs and if not, ignore it.
module.exports.run = (client, message) => {
if (['ID 1 here', 'ID 2 here'].includes(message.channel.id)) return;
};

Categories