I've been using the below script which is working fine, however I'd like to cut down on the number of times it triggers.
function sendNotification(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//var sheet = ss.getSheetByName("Allocation Requests");
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var emailAdd = "email#yourdomain.com";
if(event.range.getA1Notation().indexOf("G") > -1 && sheet.getRange("G" + row).getDisplayValue() && emailAdd.length > 1)
{
{var confirm = Browser.msgBox('Case Quantity', 'Is this a confirmed amount?', Browser.Buttons.YES_NO);
if(confirm!='yes'){return};
var rowVals = getActiveRowValues(sheet);
MailApp.sendEmail({
to: emailAdd,
subject: "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date,
htmlBody: "There has been a new allocation request from " + rowVals.name + " in the " + rowVals.team + " team.<br \> <br \> "
+ "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Issuing Depot</th><th>Delivery Date</th><th>Case Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>"
});
}
}
/**
* get values of depot, date, quantity and name from their respective cells.
* #returns {Object.<string, string>}
*/
function getActiveRowValues(sheet){
var cellRow = sheet.getActiveRange().getRow();
// get depot value
var depotCell = sheet.getRange("E" + cellRow);
var depot = depotCell.getDisplayValue();
// get date value
var dateCell = sheet.getRange("F" + cellRow);
var date = dateCell.getDisplayValue();
// get quantity value
var quantCell = sheet.getRange("G" + cellRow);
var quant = quantCell.getDisplayValue();
// return an object with your values
var nameCell = sheet.getRange("B" + cellRow);
var name = nameCell.getDisplayValue();
var teamCell = sheet.getRange("C" + cellRow);
var team = teamCell.getDisplayValue();
return {
depot: depot,
date: date,
quantity: quant,
name: name,
team: team
} }
}
Users enter more than one row of data at a time, and I would like to receive a notification when that user has finished entering data, but still send the information in the email for all the rows they've entered. At present I get an email each time they complete a row.
Related
Hi I'm trying to copy certain range of cells from google sheet to gmail using script. But it is not working especially the format is changing. How can I copy like 'ctrl+c->ctrl+v' using script? here is my code
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet(); //access to the spreadsheet
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('MAIL')); //access to the sheet by name
var range = sheet.getRange('C7:I23'); //assign the range you want to copy
var copy = range.getValues();
var sh = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sh.getSheetByName('MAIL'));
var data = sh.getRange("C7:I23").getValues();
var htmltable =[];
//var TABLEFORMAT-> // i want this format just like i made on spread sheet
//var htmltable = '<table ' +TABLEFORMAT+' ">';
for (row = 0; row<data.length; row++){
htmltable += '<tr>';
for (col = 0 ;col<data[row].length; col++){
if (data[row][col] === "" || 0) {
htmltable += '<td>' + ' ' +'</td>';
}
else if (row === 0) {
htmltable += '<th>' + data[row][col] + '</th>';
}
else {
htmltable += '<td>' + data[row][col] + '</td>';
}
}
htmltable += '</tr>';
}
htmltable += '</table>';
Logger.log(data);
Logger.log(htmltable);
var email_subject = "";
var my_email = "mail";
MailApp.sendEmail({
to: my_email,
subject: email_subject,
htmlBody: htmltable,
})}
First code is what I am currently using, I am trying to get my email address to reference in a cell. I've found another script that does this but when I try to combine them, it doesn't work.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key="
+ ss.getId() + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(ss.getName() + ".xlsx");
MailApp.sendEmail("Email Address Here", "Subject Here", "Body of Email
Here", {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
}
And trying to combine this to it to reference cells for me instead of edited the script.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Email Test'))
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Again both work on there own, but wont work when combined.
This is what I have compiled and it does send the email and attachement, but just to myself, no other recipients. Needing at least it to go to 6 other people at once.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key="
+ ss.getId() + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(ss.getName() + ".xlsx");
var sheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Email Test'))
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message, {attachments: [blob]});
}
}
catch (f) {
Logger.log(f.toString());
}
}
I'm still very new to scripting and can't work out why the below script doesn't work
function sendReturnEmail(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
if(event.range.getA1Notation().indexOf("K") && sheet.getRange("K" + row).getDisplayValue() == "Approved")
{
var rowVals = getActiveRowValues(sheet);
var aliases = GmailApp.getAliases();
var to = rowVals.user;
var subject = "Allocation request approved";
var body = "Your allocation request of " +rowVals.quantity + " cases on " + rowVals.date + " from " + rowVals.depot + " has been approved. <br \> <br \>"
+ "Regards, <br \> <br \>" +rowVals.analyst + "<br \> CPF Team";
Logger.log(aliases);
GmailApp.sendEmail(to,subject,body,
{from: aliases[0]})
;
}}
function getActiveRowValues(sheet){
var cellRow = sheet.getActiveRange().getRow();
// get depot value
var depotCell = sheet.getRange("E" + cellRow);
var depot = depotCell.getDisplayValue();
// get date value
var dateCell = sheet.getRange("F" + cellRow);
var date = dateCell.getDisplayValue();
// get quantity value
var quantCell = sheet.getRange("G" + cellRow);
var quant = quantCell.getDisplayValue();
// return an object with your values
var analystCell = sheet.getRange("J" + cellRow);
var analyst = analystCell.getDisplayValue();
var userCell = sheet.getRange("O" + cellRow);
var user = userCell.getDisplayValue();
return {
depot: depot,
date: date,
quantity: quant,
analyst: analyst,
user: user
} }
}
The sheet is question has several scripts running in it, one of them being this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2) { //checks the column
var nextCell = r.offset(0, 13);
nextCell.setValue(Session.getEffectiveUser());
}
}
I now want the sheet to send an email to the email address captured using the above script (which works fine) when the corresponding cell in column K is manually changed to "Approved."
Can anyone see what I'm doing wrong. I get no errors when it runs, but I'm not getting an email. I've searched other questions but none have helped. Many thanks in advance.
My query relates to this Google Form responses spreadsheet. I'm trying to adapt the script I got from here.
function cleanup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var InAYear = (Date.now()/86400000 + 25569) + 365;
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][5] >= InAYear) {
sheet.deleteRow(i+1);
}
}
}
I'm trying to get this to compare the date in the Start Date column of the sheet with the date in a year from now and delete the row if the column entry is greater than this (ie. if the date on the sheet is more than a year in advance). However, I obviously don't understand how to get the two different dates in the same format because examining variable values when debugging shows wildly different values.
Try the following script code:
function cleanup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'MM/dd/yyyy')
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][4] != '' && dateDiffInDays(values[i][4],today) > 365 ) {
sheet.deleteRow(i+1);
}
}
};
function dateDiffInDays(d1,d2) {
var date1 = new Date(d1);
var date2 = new Date(d2);
var timeDiff = date1.getTime() - date2.getTime();
return Math.ceil(timeDiff / (1000 * 3600 * 24));
};
It looks like I have it working, and sending me an email.
function cleanup(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var today = new Date();
var InAYear = new Date();
InAYear.setFullYear( today.getFullYear()+1 );
var emailaddress = "****";
var subject = "Annual Leave Request";
var message = "Annual Leave has been requested as follows:" + "\n\n";
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][4] > InAYear ) {
sheet.deleteRow(i+1);
subject = "Annual Leave Request - Rejected";
message = "The following annual leave request was rejected due to being more than one year in advance:" + "\n\n";
}
}
for(var field in e.namedValues) {
message += field + ':'
+ "\n" + e.namedValues[field].toString() + "\n\n";
}
MailApp.sendEmail(emailaddress, subject, message);
}
Thank you to Kishan, without who's help I would not have been able to get to this stage.
The windows.pageload event handler submits the page on page load. My intention is for this to wire the event handler to the submit event when the form submit button is clicked. What did I do wrong? Thanks!
function submit_data () {
var parent1Phone = $("parent1Phone").value;
var areaCode = parent1Phone.substring(0,3);
var prefix = parent1Phone.substring(4,7);
var suffix = parent1Phone.substring(8,12);
var firstName = $("firstName").value;
var lastName = $("lastName").value;
var sportID = $("sportID").value;
var entryFrom = $("entryFrom").value;
var linkSource = codeSource;
var primaryEmail = $("primaryEmail").value;
var graduationYear = $("graduationYear").value;
var postalCode = $("postalCode").value;
var parent1FirstName = $("parent1FirstName").value;
var parent1LastName = $("parent1LastName").value;
var parent1Relationship = $("parent1Relationship").value;
var parent1Phone1= areaCode;
var parent1Phone2 = prefix;
var parent1Phone3 = suffix;
var parent1PhoneType = $("parent1PhoneType").value; //No validation required. No non-choice allowed.
var parent1Email = $("parent1Email").value;
var parent1EmailConfirm = $("parent1Email").value;
var successDestination = success;
var errorDestination = failure;
var url = "http://recruit- match.ncsasports.org/fasttrack/saefentry/submitFullFormRemote.go?";
window.location.href = url + "firstName="+ firstName + "&lastName=" + "&lastName=" + lastName +"&sportID=" + sportID + "&entryFrom=" + entryFrom + "&linkSource=" + linkSource + "&primaryEmail=" + primaryEmail +"&graduationYear=" + graduationYear + "&postalCode=" + postalCode + "&parent1FirstName=" + parent1FirstName + "&parent1LastName=" + parent1LastName + "&parent1Relationship=" + parent1Relationship + "&parent1Phone1=" + parent1Phone1 + "&parent1Phone2=" + parent1Phone2 + "&parent1Phone3=" + parent1Phone3 + "&parent1PhoneType=" + parent1PhoneType + "&parent1Email=" + parent1Email + "&parent1EmailConfirm=" + parent1Email + "&successDestination=" + successDestination + "&errorDestination=" + errorDestination;
}
function prepareEventHandlers() {
$("frmNcsa").onclick = submit_data();
}
window.onload = function() {
prepareEventHandlers();
}
You are calling submit_data in the assignment $("frmNcsa").onclick = submit_data();, it should be $("#frmNcsa").click(submit_data);
assuming all the strings passed to the jQuery function are the ids of elements, they shold be prepended with #.