Expense approval workflow + hierarchy approval - javascript

I'm working to expand on the workflow script posted here:
James Ferreira Workflow video.
function sendEmail(e) {
var email = e.values[1];
var Item = e.values[2];
var cost = e.values[3];
var vendor = e.values[4];
var url = '<SPREADSHEET>';
var approve = url + '?approval=true'+'&reply='+email;
var reject = url + '?approval=false'+'&reply='+email;
var html = "<body>"+
"<h3>Please review</h3><br />"+
Item +": " + cost+ ": " + vendor+ "<br />"+
"Approve<br />"+
"Reject<br />"+
"</body>";
MailApp.sendEmail("<MY EMIAL>", "Approval Request",
"What no html?", {htmlBody: html});
}
function doGet(e){
var answer = (e.parameter.approval == 'true') ? 'Buy it!' : 'Not this time, Keep saving';
MailApp.sendEmail(e.parameter.reply, "Purchase Request",
"Your manager said: "+ answer);
var app = UiApp.createApplication();
app.add(app.createHTML('<h3>An email was sent to '+ e.parameter.reply + ' saying: '+ answer + '</h3>'))
return app
}
Question 1) How do you build in an additional approval flow dependent on the cost?
Question 2) Depending on the approval amount how can I adjust the manager emaill?
For example: if the cost is above £500 I would like to send the approval email to another email account / manager. Would be amazing if the script could automatically recognise this.
Any support of this is much appreciated,
Andrew

Related

Node.js: There's something wrong with this, and I don't know what

I'm helping my friend with some code, and I tried to fix the problem in it, but neither of us can figure it out, so here I am, asking people on StackOverflow for help!
var colors = require('colors/safe');
var chalkAnimation = require('chalk-animation');
var nodemailer = require('nodemailer');
var Username = prompt('[ⓘ SET UP INFO ] - Please start the connect process by typing in: "mh!startup" ');
if (Username == 'mh!startup') {
const RainbowMPPHAXX = chalkAnimation.rainbow(`███╗░░░███╗██████╗░██████╗░  ██╗░░██╗░█████╗░██╗░░██╗██╗░░██╗
████╗░████║██╔══██╗██╔══██╗  ██║░░██║██╔══██╗╚██╗██╔╝╚██╗██╔╝
██╔████╔██║██████╔╝██████╔╝  ███████║███████║░╚███╔╝░░╚███╔╝░
██║╚██╔╝██║██╔═══╝░██╔═══╝░  ██╔══██║██╔══██║░██╔██╗░░██╔██╗░
██║░╚═╝░██║██║░░░░░██║░░░░░  ██║░░██║██║░░██║██╔╝╚██╗██╔╝╚██╗
╚═╝░░░░░╚═╝╚═╝░░░░░╚═╝░░░░░  ╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝ `);
}
setTimeout(function() {
RainbowMPPHAXX.start(); // Animation resumes
}, 0);
setTimeout(function(){ console.info(colors.cyan("[ ⓘ CONNECT INFO ] - Connecting to MPP HAXX . . . ")); }, 700);
setTimeout(function(){ console.info(colors.green("[ ⓘ CONNECT INFO ] - Successfully connected to MPP HAXX! ")); }, 1700);
setTimeout(function(){ console.info(colors.cyan("[ ⓘ CONNECT INFO ] - Connecting to MPP HAXX login . .")); }, 1800);
setTimeout(function(){ console.info(colors.green("[ ⓘ CONNECT INFO ] - Successfully connected to MPP HAXX login! start up successful! :D")); }, 2000);
/*Thanks Alex for helping with some wrapping in the code!*/
/*MPP HAXX LOGIN*/
var KnowsUsername = true;
var KnowsEmail = true;
var KnowsPassword = true;
setTimeout(function () {
var Username = prompt('[LOGIN QUESTION] - What is your username?');
if (Username == '') {
KnowsUsername = true;
console.info(colors.green('[ ⓘ LOGIN INFO] - Correct username!'));
var Email = prompt('[LOGIN QUESTION] - What is your E-mail?');
if (Email == '') {
KnowsEmail = true;
console.info(colors.green('[ ⓘ LOGIN INFO] - Correct E-mail!'));
var Password = prompt('[LOGIN QUESTION] - What is your password?');
if (Password == '') {
KnowsPassword = true;
const RainbowLoading = chalkAnimation.rainbow(`Loading...`);
setTimeout(function() {
RainbowLoading.start(); // Animation resumes
}, 0);
console.info(colors.green('[ ⓘ LOGIN INFO] - Correct password!'));
setTimeout(function(){console.info(colors.cyan(`[ ⓘ 2FA INFO ] - This is the final last step of the verification process! since you have gotten all of the login questions correct so far. We will now send you an E-mail to your E-mail: "${Email}" with the 2FA code. If you don't recive it in your main inbox please click on this link: https://mail.google.com/mail/u/0/?tab=rm&ogbl#spam `)); }, 7000);
} else {
KnowsPassword = false;
console.error(colors.red('[LOGIN ERROR] - Aww! it looks like ' + '"' + Password + '"' + " is not your password. Try again!" ));
}
} else {
KnowsEmail = false;
console.error(colors.red('[LOGIN ERROR] - Aww! it looks like ' + '"' + Email + '"' + " is not your E-mail. Try again!" ));
}
} else {
KnowsUsername = false;
console.error(colors.red('[LOGIN ERROR] - Aww! it looks like ' + '"' + Username + '"' + " is not your username. Try again!" ));
}
}, 2100);
He uses VSC (Visual Studio Code) to see what it does, and apparently it doesn't give the "Loading..." thing rainbow text, I'm hoping somebody knows what's going on, because I don't

Google Script email send ignoring #N/A values in row

Essentially i'm also pulling email addresses from a separate sheet via a formula so that when new form entry is made, column B populates the proper email (name is included in the form). The problem I'm facing is that for the tool to be efficient I need to run the formula all the way down indefinitely so that when new entry comes it simply looks up the name and column B pulls the correct email address. However, the below code returns an error because it cannot handle #N/A where no email exists yet or even "" with IFERROR in the formula.
Ideally the code will stop if #N/A is encountered and only continues when there is an email in CurrentRow[1] has an email. The code works perfectly fine if empty rows are totally blank.
The current version here comes from what I've read and researched, including the post: Email only new rows in Google sheets. I've researched and lots of similar examples exist but having tried 40 or so variations i could not get mine to work.
function sendEmail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var StartRow = 2;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
var AllValues = WholeRange.getValues();
var message = "";
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//define column to check if sent
var KamEmail = CurrentRow[1];
//if row has been sent, then continue to next iteration
if (CurrentRow[1] == "#N/A") {break;}
else;
//set HTML template for information
message +=
"<p>Hi " + CurrentRow[1] + "</p>" +
"<p>Your constituent has submitted an Agenda blog pitch" + "</p>" +
"<p><b>Name of constituent: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Submission Snapshot: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Link to Submission: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Date of Submission: </b>" + CurrentRow[7] + "</p><br><br>";
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 9).setValue("Sent to KAM");
}
//define who to send grants to
var SendTo = CurrentRow[1];
//set subject line
var Subject = "RE: Registrations";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: Subject,
htmlBody: message,
});
}

In Sheet, Send email to a specific recipient when a cell is filled

I am trying to create a sheet for project management to collaborate easily among many interlocutor (I know there are free tools outhere working with a public organization is hellish to insert new procedures into the project).
Here is the sample file created : Google Sheet for Test
The ideais everytime a new question is input, the author can select the menu "Question" > "Send Question".
And it should sent automatic email to the Recipient email address (column C)... under the condition that the Question cell is filled (condition checked in "I2").
But currently, the script does not work as expected.
Note: I am currently learning JS.
Here is the code :
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Question')
.addItem('Send Question', 'sendQuestion')
.addSeparator()
.addItem('Send Answer', 'sendAnswer')
.addToUi();
}
function sendQuestion() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Questions");
var range = sheet.getRange("A22:L2");
var emailvalid = range.getCell(1,7).getValue();
var question = range.getCell(1,5).getValue();
var answer = range.getCell(1,6).getValue();
var qvalid = range.getCell(1,8).getValue();
var ansvalid = range.getCell(1,10).getValue();
if(qvalid == "Nope"){
Browser.msgBox("Please, write a question !");
}
else {
return false};
if(emailvalid == "Nope"){
Browser.msgBox("Please, input a valid email address !");
}
else {
return false};
var from = range.getCell(1,1).getValue();
var recipient = range.getCell(1,2).getValue();
var emailTo = range.getCell(1,3).getValue();
var subject = "Project Management | Your received a new question" + question;
var link = "https://docs.google.com/spreadsheets/d/1QUbw0WNju55h5pk3l8QqVCYa_jSCzrIzMjf0N4v-z8c/edit?usp=sharing"
var options = {}
options.htmlBody = "Hi" + recipient +"," + "<br />"+ "<br />" + "You received a new question from " + from + "<br />" + "<br />" + "You can answer directly into the table here: "<"br />"+"<br />" + link + "<br />" + "<br />"+ "Thank you";
GmailApp.sendEmail(emailTo, subject, " ", options);
}
function emailSent(){
var ui = SpreadsheetApp.getUi();
var sent = sendQuestion("Send Email ?",ui.ButtonSet.YES_NO);
if(sent == ui.Button.YES) {
ui.alert("Your Message has been sent.");
} else {
ui.alert("Email canceled");
}
}
You're main error there is this else return false statement you have, which is basically terminating the function if your validation passes, where it should be the other way around, exiting the function if the validation fails, after you popup informing the problem.
if(qValid == 'Nope') {
Browser.msgBox('etc...');
return; //return here, not in the else clause
}
//same for email valid
There are quite a few other "improvements" I'd do on such function. But I guess it's beyond the point here, you're learning as you go and the most important is that it works now.

Fixing Syntax Error, on my Script Editor

function sendEmails(e){
var UserName = e.values[3];
var UserEmail = e.values[5];
var date = e.values[0];
var pin = e.values[4];
var subject = "[LFR] Application Succeed!";
var bodymessage = "Hi" + UserName + "for sending in your job application."
<br>
<br> + "The following is your Purchase Log Pin"
<br> + pin;
var emailsent = "Email Sent";
var isEmailsent = e.values[6];
if (isEmailsent ! = emailsent);
MailApp.sendEmail(UserEmail, subject, bodymessage);
}
The error message always shows, "Illegally formed XML syntax. (line 18, file "Code"). Someone please help me to fix it!
change this
var bodymessage = "Hi" + UserName + "for sending in your job application."
<br>
<br> + "The following is your Purchase Log Pin"
<br> + pin;
to
var bodymessage = "Hi" + UserName + "for sending in your job application.<br> <br>The following is your Purchase Log Pin<br>" + pin;
bascially, your string is not formed correctly
Also,
remove ; after the if statement
if (isEmailsent ! = emailsent)
{
MailApp.sendEmail(UserEmail, subject, bodymessage);
}

Google Apps Script: Workflow approval Selecting approval broken

I'm working to expand on the workflow script posted here...
James Ferreira Workflow video.
I've expanded the script and all is working fine regarding email, but I'm running into an issue with the doGet() function not working corretly.
Here is the code from the video: (mycode for the sendEmail is different and works fine, as it uses and document template.
function sendEmail(e) {
var email = e.values[1];
var Item = e.values[2];
var cost = e.values[3];
var url = '<ENTER YOUR PUBLISHED URL>';
var approve = url + '&approval=true'+'&reply='+email;
var reject = url + '&approval=false'+'&reply='+email;
var html = "<body>"+
"<h2>Please review</h2><br />"+
Item +": " + cost+ "<br />"+
"Approve<br />"+
"Reject<br />"+
"</body>";
MailApp.sendEmail("jjones#beaconcloudsolutions.com", "Approval Request",
"What no html?", {htmlBody: html});
}
function doGet(e){
var answer = (e.parameter.approval == 'true') ? 'Buy it!' : 'Not this time, Keep saving';
MailApp.sendEmail(e.parameter.reply, "Purchase Request",
"Your manager said: "+ answer);
var app = UiApp.createApplication();
app.add(app.createHTML('<h2>An email was sent to '+ e.parameter.reply + ' saying: '+ answer + '</h2>'))
return app
}
So when the script runs, i get the email that shows the links for approval / rejected, but when I select the link for approval or / rejected i get
"Sorry, the page (or document) you have requested does not exist." " Please check the address and try again."
This link should be redirect to the published webapp, noted in the var URL and append either the var approve or reject to it and then just display a basic HTML page.
Any suggestions on what is wrong? the script is authorized..
What does the address bar say when you click on the link from the email? Perhaps something is malformed or not properly encoded.
My gut feeling right now is that there is a missing '?' before you append any params.
Perhaps try this -
//note the ? instead of &
var approve = url + '?approval=true'+'&reply='+email;
var reject = url + '?approval=false'+'&reply='+email;

Categories

Resources