I'm trying to run a check that would send an email automatically if the due date is tomorrow on a piece of equipment. The problem is that I'm not sure if I can possibly use this -> https://developers.google.com/apps-script/reference/spreadsheet/relative-date?hl=ru To run the check.
This is what I have so far, hopefully someone can help me here, I'm still fairly new to app script, and am honestly not the best coder in general. Combine that with my inability to find good examples for app script, and my problems are fairly compounded.
function emailForLaptops() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Main');
var tomorrow = new Date(TOMORROW);
var active = sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data = sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
if (row[1] == tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}
function emailForLaptops() {
var ss=SpreadsheetApp.getActive();
var sheet1=ss.getSheetByName('Main');
var dt=new Date();
var tomorrow=Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+1),Session.getScriptTimeZone(),"MM/dd/yyyy");
var active=sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data=sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
var r1dts=Utilities.formatDate(new Date(row[1]),Session.getScriptTimeZone(),"MM/dd/yyyy");
if (r1dts==tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}
Related
this is my app script function where I am updating the google spreadsheets. I am getting the all the Html user filled data separated by "/".
I am sending a response from an Html page to google spreadsheet and want to
get the milliseconds details on date object, to avoid the overwriting.
Let's say if multiple people are submitting at once then, I can get only
the last entry because the date object is created with mm/dd/yyyy hh:mm:ss
format and if multiple people are submitting at the same instance of time
like ( 4 people are submitting at 03/03/2020 04:55:55) then I will be
getting the last submitted record and other records will be overwritten by
the last record.
Can anyone of you help me with this? How should I solve this requirement?
Please find my javascript function from the HTML page where I am calling google app script API to run the function userClick written in app script.
function sendData(u_wk_Det)
{
google.script.run.withSuccessHandler(function(response)
{console.log(response);})
.withFailureHandler(function(err){console.log(err);})
.userClick(u_wk_Det);
}
/*code shown below is my app script function using which I am updating the google spreadsheets.*/
`function userClick(u_wk_det)
{
Logger.log('table data - ');
var ss =
SpreadsheetApp.openById('sheetid').getSheetByName('Sheet2');
var Srow = 2;
var Lrow = ss.getLastRow();
var Lcol = ss.getLastColumn();
var data = ss.getRange(2, 1, Lrow , Lcol).getValues().filter(function(o)
{return o !=="" });
for(i in data)
{
var j=i;
j++;
j++;
var Crow = data[i];
if (Crow[0]=='')
{
var dt = new Date();
Logger.log(dt);
Logger.log(Crow[0]);
if(u_wk_det!=="")
{
var user_week_details = u_wk_det.split("/");
ss.getRange(j,1).setValue(dt);
ss.getRange(j,2).setValue(user_week_details[0]);
ss.getRange(j,3).setValue(user_week_details[1]);
ss.getRange(j,4).setValue(user_week_details[2]);
}
}
}
}`
I am working on a Google Form that allows our employees to submit an in-field inspection of their equipment. I have a script that takes the form responses and creates a new sheet based on the date and the specific unit number of the equipment. The user goes through a checklist and selects either "Good" or "Needs Repair" for each item on the list. They can also add comments and upload pictures of any issues.
I am trying to have the script automatically send an email if "Needs Repair" is selected for any of the checks, as well as if the user adds a comment or a picture. This way we do not have to open every submitted sheet to know if any repairs are required. What I have is just not sending emails and I cannot figure out why. Any help is greatly appreciated!
Here is my current script:
function onFormSubmit() {
// onFormSubmit
// get submitted data and set variables
var ss = SpreadsheetApp.openById("*Spreadsheet Link*");
var sheet = ss.getSheetByName("Submissions");
var row = sheet.getLastRow();
var Col = sheet.getLastColumn();
var headings = sheet.getRange(1,1,1,Col).getValues();
var lastRow = sheet.getRange(row, 1, 1, Col);
var UnitNumber = sheet.getRange(row,3).getValue();
var newSheet = sheet.getRange(row,4,Col).getValue();
var fileExist = false;
var drillSheet = null;
var folder = DriveApp.getFoldersByName("Fraser Drill Inspections").next();
var files = folder.getFilesByName(UnitNumber);
var file = null;
var employee = sheet.getRange(row,2);
var checks = sheet.getRange(row, Col, 1, 20);
// check if Drill has sheet
while (files.hasNext())
{
fileExist = true;
file = files.next();
break;
}
if (fileExist) //If spreadsheet exists, insert new sheet
{
drillSheet = SpreadsheetApp.openById(file.getId());
drillSheet.insertSheet("" + newSheet);
}
else //create new spreadsheet if one doesn't exist
{
drillSheet = SpreadsheetApp.create(UnitNumber);
var ssID = drillSheet.getId();
file = DriveApp.getFileById(ssID);
file = file.makeCopy(UnitNumber, folder);
DriveApp.getFileById(ssID).setTrashed(true);
drillSheet = SpreadsheetApp.openById(file.getId());
drillSheet.renameActiveSheet(newSheet);
}
// copy submitted data to Drill sheet
drillSheet.getSheetByName(newSheet).getRange(1,1,1,Col).setValues(headings);
drillSheet.appendRow(lastRow.getValues()[0]);
drillSheet.appendRow(['=CONCATENATE(B6," ",B5)']);
drillSheet.appendRow(['=TRANSPOSE(B1:2)']);
//Hide top rows with raw data
var hiderange = drillSheet.getRange("A1:A3");
drillSheet.hideRow(hiderange);
//Widen columns
drillSheet.setColumnWidth(1,390);
drillSheet.setColumnWidth(2,700);
//Send email if there are any comments or if anything needs repair
if(lastRow.getValues() == "Needs Repair") {
function SendEmail() {
var ui = SpreadsheetApp.getUi();
MailApp.sendEmail("email#domain.com", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.")
}
}
}
The function to send an email is:
GmailApp.sendEmail(email, subject, body);
Try changing
if(lastRow.getValues() == "Needs Repair") {
function SendEmail() {
var ui = SpreadsheetApp.getUi();
MailApp.sendEmail("email#domain.com", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.")
}
}
to just the following:
if(lastRow.getValues() == "Needs Repair") {
GmailApp.sendEmail("youremail#domain.com", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.");
}
It looks like you've still got some additional work to do too, e.g. to make it send to the email address from the form submission instead of a hardcoded one.
I have a script to send emails but it is not sending as per my desired format.
Can it be possible to send emails like this:-
Now my script is sending like this:-
TF,Unfunded,Platform,Link,,TF,Unfunded,,Data,Link,Date,Round,Funds,Domain,,Date,Round,Fundings,Domain,Changes Made,,,,,,7-May-02,Incorporation,2037.41,Shaji Vadakke Parambath,Vinay Gupta,Add round,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
My code is:
You can use a loop like this. If you want to get a better response, in the future I would recommend giving us access to a spreadsheet that has data consistent with the example and access to the script editor. It's difficult to copy code or data from an image. Which makes it less likely that someone will step up to help you. It would also be appreciated if you would take a stab at the code yourself. This code does work and I tested it with fake data.
This will comma separate all of the values in the row that it sends. The last column must be empty to qualify for sending and the 8 column must not be blank.
function SendingEmails()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var startRow=5;
var rg=sh.getDataRange();
var vA=rg.getValues();
var subject='Your Subject';
var emailaddress='Your Email Address';
for(var i=startRow-1;i<vA.length;i++)
{
if(!vA[i][vA[0].length-1] && vA[i][7])//not already sent and some data is available
{
//Logger.log('Send Mail: %s',formatAsTable(vA[i]));
//var userInterface=HtmlService.createHtmlOutput(formatAsTable(vA[i]));
//SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Format As Table')
MailApp.sendEmail(emailaddres,subject,vA[i].join(','))
sh.getRange(i+1,vA[0].length).setValue('mailsent');
}
}
}
function formatAsTable(vA)
{
var s='<style>table{border:1px solid #000;}th,td{border:1px solid #000;}"</style><table><tr>';
for(var i=0;i<vA.length-1;i++)
{
s+='<td>' + vA[i] + '</td>';
}
s+='</tr></table>';
return s;
}
I suggest you install SheetConverter (created by David Bingham (Mogsdad)). Google it and it gives great instructions on how to install the library. The, I modified the example given to this:
function example() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveSheet().getDataRange();
var conv = SheetConverter.init(ss.getSpreadsheetTimeZone(),
ss.getSpreadsheetLocale());
// Grab an array for formatted content
var array = conv.convertRange(range);
// Get a html table version, with all formatting
var html = conv.convertRange2html(range);
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Daily report','' ,{htmlBody: html})
}
It sent a test I set up of your data perfectly.
I'm working with the "new" version of google SS.
I'd like to get the form attached to the spreadsheet I'm in, like this:
function findFormURL() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
return ss.getFormUrl();
}
However, this function does not work yet in the new version.
Anyway,it gives the URL of the form, which is interesting, but I'd like to have the form ID or object so I can then work with it, change some stuff etc. Is that possible ?
This is indeed annoying but there is a possible way to get around this missing feature using the drive search capabilities... I tested it with the code below and it worked.
I agree that this is far from ideal and requires to have a form that has the same unique name as your spreadsheet but it's better than nothing.
function getFormTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var nameToSearch = ss.getName();
Logger.log('title contains "'+nameToSearch+'"');
var forms = DriveApp.searchFiles('title contains "'+nameToSearch+'"');
while (forms.hasNext()){
var formDoc = forms.next();
Logger.log(formDoc.getMimeType());
if(formDoc.getMimeType()=='application/vnd.google-apps.form'){
break;
}
}
Logger.log('formDoc = '+formDoc);
var form = FormApp.openById(formDoc.getId());
var items = form.getItems();
for(var i in items){
Logger.log(items[i].getTitle()+' '+items[i].getType());
}
}
I have this right now as a script to send an email to myself as a test showing that there is a new application submitted, but I am looking to have a full-fledged email sent to our HR department giving the name of every field following by a : and a space then what their answer was. I know you can do this and have found a script that I tried to get to work, but it kept failing and the developer was no help in trying to help me fix it. I really would love to get this done for my HR department so that we can move on from creating a new application. Here is my code so far, I removed my email for privacy issues:
function sendFormByEmail(e){
var email = "emailgoeshere";
var subject = "A New Employment Application has been Submitted";
var message = "A New Application Has Been Submited. Please go to the spreadsheet for more details.";
MailApp.sendEmail(email, subject, message);
}
Thanks again!
Or you could try this very simple one :(read carefully comments in the script)
function sendFormByEmail(){
var email = "email adress comes here";
var subject = "A New Employment Application has been Submitted";
var message = "A New Application Has Been Submited. Please find the details below :";
var row = SpreadsheetApp.getActiveSpreadsheet().getLastRow();// assuming data is on the last row of the spreadsheet
var messagebody=createmessage(row);
MailApp.sendEmail(email, subject, message+messagebody);
}
//
function createmessage(row){
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastCol = ss.getLastColumn();
var LF= "\r\n"
var body = LF
var headers = sh.getRange(1,1,1,lastCol).getValues();
var data = sh.getRange(row,1,1,lastCol).getValues();
for(nn=0;nn<headers[0].length;++nn){
body+=headers[0][nn]+" : "+data[0][nn]+LF
}
Logger.log(body)
return body
}
//
//eof
As mentionned, data coming from the form must be on last row of data in the sheet and there must be some data to check how it works otherwhise answers=questions.
I've developed a script that might help you, it's called FormEmailer. You can find it in the Script Gallery or grab the code on its site.