I'm trying to set up a workflow using Google Forms, Sheets and Apps Script. For a simple example lets say I am setting up a leave form for a company. I have a form for the employee enters their leave details. On submit the details are populated into a sheet and an email is sent to an someone for approval.
This is where I am getting stuck. What is the best way to approve the leave requests? I can just edit the sheet and once the column has been changed from unapproved to approved and email could be sent but I was hoping to avoid having to edit the sheet at all.
Is there a better way to handle this workflow?
Assume that 3 people need to approve the request. The approvers that need to review the request are approvers X,Y,Z.
User X gets an Email requesting that they review the request
The email that user X gets will use HTML
The HTML will have a link in it
A link in HTML submits a GET request
Whenever you click a link, or enter a url into the browser address
bar, a GET request is made to the url
An Apps Script Web App is an Apps Script project that is published as
a Web App
A Web App has a url that can be used to make a request to it
When the url (link) is used, a GET request is made
A GET request made to an Apps Script Web App will run the doGet()
function
Once that doGet() function run, you can have your Apps Script code do
anything that you want
It can get information sent in to it from a search string
Then send out another email to user Y
So, when user X clicks the link, the url needs to have information
appended to the url in the form of a search string
httpz://the link?whichUser=X&approved=true
You can get the user and whether it was approved or not from the "event object" often designated by the letter "e"
function doGet(e) {
var whatUserJustReviewed,isItApproved;
//Get the values passed in from "e" which is the event object
eventParam = e.parameter;
whatUserJustReviewed = eventParam.whichUser;
Logger.log(' whatUserJustReviewed : ' + whatUserJustReviewed )
isItApproved = eventParam. approved;
switch (whatUserJustReviewed) {
case "X"
if (isItApproved === 'true') {
// //Send an email to the next user which is Y
}
break;
case "Y"
if (isItApproved === 'true') {
// //Send an email to the next user which is Z
}
break;
case "Z"
if (isItApproved === 'true') {
//Send an email to all involved who need to know that it was approved.
}
break;
default:
console.error('There was an error getting user or status');
};
}
Related
I have an HTML form which saves its responses to a Google Sheet. The form contains the following fields:
Name:
Email:
Subject:
Message:
Now, what I want is I want to automate sending a thank you email to a recipient as soon as he/she fills the form to the address mentioned in the "Email" field. I don't want to use Google Forms as the backend as it is hard to bypass the "Response Recorded" Confirmation page. Also, avoiding PHP would be better for me!
Is there a way to send these e-mails automatically? The format of the email is as follows:
From: <my email address>
To: <email address from the "Email" field in the spreadsheet>
Subject: Re: Submission Received
Hey <name from the "Name" field in the spreadsheet>!
Body of the mail
If there is a way to bypass the confirmation page of Google Forms, please let me know! That would also do!
If you have the data in google sheets, then you can handle your automation there. I setup this sample file that you could probably base your data set on.
From there I attached the below script to the spreadsheet. You would then need to set a trigger to execute this script on a somewhat regular frequency (5 minutes? 1 minute?). I think it should accomplish what you are going for. There's a check built in to ensure that partial data is not sent.
const ss = SpreadsheetApp.getActiveSheet();
const sentMessageColumn = ss.getRange("E:E").getColumn();
function regularProcedure(){
var aCell = ss.getRange(ss.getLastRow(),sentMessageColumn)
while(aCell.isBlank()){
var eRow = aCell.getRow();
if(!(ss.getRange(eRow,2).isBlank() ||
ss.getRange(eRow,3).isBlank() ||
ss.getRange(eRow,4).isBlank()))
{
var newMail = GmailApp.createDraft(
ss.getRange(eRow,2).getValue(),
ss.getRange(eRow,3).getValue(),
ss.getRange(eRow,4).getValue());
newMail.send();
aCell.setValue(true);
}
aCell=aCell.offset(-1,0);
}
}
New user here and not versed in code much. Im working on a COVID-19 form for our company and looking for some help with google script/trigger where when an employee fills out the google form and selects yes/no on the google form the google sheet that collects the data will send off an email based on the value in the cell.
IE: employee A enters "no" to agreeing to comply to policy it will email the manager informing them that someone entered "no".
I have a test formula that is working but when I set up a min by min trigger for it to kick off it just continues to kick off. Im assuming this is obviously due to it not having a code to only send new entries??
Any help would be greatly appreciated.
To reiterate im trying add a script that sends an email only ONCE per user when someone fills out the google form and they choose the wrong answer.
Code I have now:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var currentValue = sheet.getRange("F2:F1000").getValue();
if (currentValue = ("Yes")) {
MailApp.sendEmail("test#example.com", "ALERT: Please see person in question!", "The message body that you want to send.");
}
}
Try this:
You haven't identified the person in question.
function onMyEdit(e) {
const sheet=e.range.getSheet();
if(sheet.getName()=='Sheet1') && e.range.columnStart==6 && e.range.rowStart>1 && e.value=='No') {
//MailApp.sendEmail("test#example.com", "ALERT: Please see person in question!", "The message body that you want to send.");
Logger.log("ALERT: Please see person in question!");
}
}
Since this is sending an email which requires authorization you will have to create an installable trigger using the Edit/Current Project Triggers menu.
I have an email typed into field in my html file. I also have the user email captured by the Code.gs file.
I have figured out how to check to make sure these two match. However, I cannot figure out how to make a pop-up message (alert of any kind) appear on the user's screen when these do or do not match. (EX: 'user email does not match typed email' when failed and 'successful match' when it match)
if(userEmail == typedEmail){
Logger.log('Success!');
}
else{
Logger.log('Fail!');
}
I would like to add a message before Logger.log('Success!'); and Logger.log('Fail!');
The Logger.log is working correctly.
Requirement:
Send an alert to the spreadsheet user if email addresses don't match.
Solution:
Since I don't have the rest of your function I can't format the code too well, but this section should look something like this to achieve your goal:
var ui = SpreadsheetApp.getUi();
if(userEmail == typedEmail){
ui.alert('Successful match');
Logger.log('Success!');
}
else{
ui.alert('User email does not match with typed email');
Logger.log('Fail!');
}
Explanation:
We can use ui.alert() to send an alert to the user of the spreadsheet, all you need to do is define the message inside your brackets. I copied the ones from your question as an example.
References:
Class Ui Documentation
I seek an approach by which I can provide user a bespoke Google Sheet dashboard on the basis of some ID that is entered or transferred via URL.
To explain: as of now, raw data sits in a master Google Sheet and is processed and summarised in another Google Sheet dashboard that requires to enter an ID which acts as filter to the raw data so that the summary only presents insights associated with that particular ID and user - that works.
However, Each user should enter only their ID and see their summary. Right now all users have access to the same public Sheet and the possibility of parallel access is problematic.
How may I generate individual Sheets (one per user) that is based on a template?
Is this possible with default functionality, or Apps Script? Any advice is highly appreciated, thank you!
This is one option you can try, you can get the email ID of the user who opens the google spreadsheet like so:
function onOpen(e) {
var email = Session.getEffectiveUser().getEmail()
var ui = SpreadsheetApp.getUi()
ui.alert(email)
//doSomethingSpecificBasedOnEmail(email) call function that fliters data based on email ID
}
Note: There are few nuances to using Session.getEffectiveUser(). Based on permission and security setting, it can give you a blank user/email.
https://developers.google.com/apps-script/reference/base/session#getActiveUser()
Second Option:
If email ID is not an option and since you are ok with users entering an ID to access the data. This code will ask for an ID and create a copy of sheet called template and also set the value of A1 as the ID. The sheet can then use the ID to get ID specific data and make plots.
function onOpen(){
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Get ID:');
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
var id = response.getResponseText();
var ss = SpreadsheetApp.getActive()
var lookupSheet = ss.getSheetByName(id)
if(lookupSheet == null){
var template = ss.getSheetByName("Template")
var newSheet = template.copyTo(ss)
newSheet.setName(id)
newSheet.getRange(1,1).setValue(id)
newSheet.activate()
} else
{
lookupSheet.activate
}
} else {
Logger.log('The user clicked the close button in the dialog\'s title bar.');
}
}
An example of this can be found here: https://docs.google.com/spreadsheets/d/1RPHUGKi7u9jJVc-3xCaYlrZ8kaHWvpl6gPZxUL1g32Y/edit?usp=sharing
Note: People can see each others sheet though, which I assume based on your post is not an issue. However, if that is not the case the best option is to use google Web App script.
I have some form on my pure JS/HTML/CSS site (without server side). And have Google account. My purpose is send filled form from web client direct to Google Spreadsheet. Is it possible?
Yes, this is possible. I would do it by creating an Apps Script and deploying it as a web app. Here is a sample script: fill in the Id of the spreadsheet and the name of one of its sheets.
function doPost(event) {
var params = event.parameter;
var sheet = SpreadsheetApp.openById('..Id..').getSheetByName('..Name..');
if (params.data !== undefined) {
sheet.getRange(1, 1).setValue(params.data);
return ContentService.createTextOutput("Success");
}
else {
return ContentService.createTextOutput("Oops");
}
}
Publish the script as a web app, allowing access to everyone (unless you want to deal with authentification). This will give you a URL such as https://script.google.com/macros/s/..../exec
On the client side, you send POST request to that URL. The script expects data to be in parameter "data".
var url = 'https://script.google.com/macros/s/..../exec';
$.post(url, {data: 'hello world'});
(I'm using jQuery here just to have a shorter example; you can send a POST request in plain JavaScript.)
The cell A1 of the sheet you chose will have "hello world".