How can I send url of my Spreadsheet in email - google-apps-script

How can I send url of my Spreadsheet by email ?
My code :
var emailAddress = "my email";
var url = "";
var subject = nom1;
MailApp.sendEmail(emailAddress, subject,url);
This code which send an email works.
But how can i get my url(speadsheet) ?

Assuming you already have your spreadsheet created, do the following steps:
Open your spreadsheet in google drive
Go to File->Share, a dialog should pop up with your share settings
At the top there should be a URL that you can send to people.
Some additional Notes:
If your spread sheet is set to private, you can change the settings such that only people who have the link are able to view the spreadsheet.
EDIT:
There is also getURL() in the apps script api
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getUrl()
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getUrl());
So in your case:
MailApp.sendEmail(emailAddress, subject,ss.getUrl());

Related

Can I send multiple uploaded attachments from a Google Form/Google Sheet in an automated email?

I am trying to create a Google Form linked to a Google Sheet utilizing some Google App script that will take some answers from a survey, attach an uploaded file, and send an email to a specific person. I have been able to figure out the part where I collect the data in a Google Sheet and send an email, but the part where I take the uploaded file and have it as an attachment in the email is stumping me.
Currently, my code to send the email looks like this:
GmailApp.sendEmail(Recipient,subject,'',{htmlBody: htmlText},);
But looking at the documentation on sendEmail, it looks like I want to add more to that Options part, right? The so if I am defining a variable for this, I need to use getFileById, but the file ID will be different with each upload. Furthermore, I might need to attach multiple files.
I have created a test Google Form here and I have attached it to a Google Sheet here. You can see the Google App Script here. You can check the email being sent/received successfully by looking at formtesting4#mailinator.com as specified in the code.
Is that possible with what I am trying to do?
You can refer to this sample script:
function emailMe() {
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const info = ss.getDataRange().getValues();
info.forEach((entry,a) => {
// Identify whether notification has been sent
if (entry[4] === '') {
// Collect entries
var Recipient = "your#email.com"
var subject = 'Roster Scheduler Update';
Timestamp = entry[0];
Name = entry[1];
Roster = entry[2];
attachment = "TEST";
var attachmentBlobs = [];
//Get the blob of all attachments available
if(entry[3]){
var attachments = entry[3].split(', ');
attachments.forEach(url => {
var fileId = url.replace('https://drive.google.com/open?id=','');
Logger.log(fileId);
var file = DriveApp.getFileById(fileId);
attachmentBlobs.push(file.getBlob());
});
}
let body = '';
// Generate email
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
// Send email
GmailApp.sendEmail(Recipient, subject, body, {htmlBody: htmlText, attachments: attachmentBlobs});
// Email confidence
const check = 'Sent';
ss.getRange(a + 1,5).setValue(check);
}
});
}
Changes done:
Get the blob file of all the uploaded attachments using File.getBlob(). You can get the file id from the attachment's url link and use DriveApp.getFileById(id)
Include body in the GmailApp.sendEmail() to fix email content issue
Include attachments as an option in the GmailApp.sendEmail(recipient, subject, body, options)
Output:
Sample1: 3 attachments
Sample2: 1 attachment

Paste image in email before sending, google app scripting

Google App scripts noob here. I am trying to send email from google sheets and I have a working code. However, the challenge I have unaddressed is to paste an image in the email before sending it. The image that needs to be pasted is copied from the snipping tool. In Microsoft Outlook, I have used .Show function to preview the email and paste the image but I am unable to achieve the same in google app scripting. The image is different every time an email is sent and saving the image to a drive location defeats the automation effort so the preferred option is to paste the image before its sent. I would appreciate any suggestions and recommendations to make this happen. I am sure there is a way to do this and I am at the right place to find that way.
function SendNote() {
var active_range = SpreadsheetApp.getActiveSpreadsheet().getActiveRange();
var env_type = SpreadsheetApp.getActiveSheet().getRange(active_range.getRowIndex(), 1).getValue();
var count = SpreadsheetApp.getActiveSheet().getRange(active_range.getRowIndex(), 4).getValue();
var mail_type = SpreadsheetApp.getActiveSheet().getRange(active_range.getRowIndex(), 13).getValue();
// Check totals sales
if (mail_type = 'Kick_Off')
{
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
var emailAddress = emailRange.getValues();
// Send Email.
var message = 'This is my message ' + env_type; // Second column
var subject = 'This is the subject';
MailApp.sendEmail('myhandle#mymail.com', subject, message);
}
}

Google apps scripts : get the URL of the spreadsheet and send it by mail

I created a button in my spreadsheet: I want it to send the URL of the spreadsheet when I click on it, but unsuccessfully right now.
Here is my code:
function mail() {
var destID = '1kVhuTwVr80AScne9ijtlWs9YlDf5YkixIFVVbPjoX5E';
var recipient = Session.getActiveUser().getEmail();
var sprURL = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(sprURL.getUrl());
GmailApp.sendEmail(recipient, 'SUBJECT OF THE MAIL', 'The Spreadsheet sits at the following URL: ' + Logger.log(sprURL.getUrl()))
}
Where am I going wrong?
Thanks a lot guys.
the error is that you are pasing logger.log(url) when you build the subject string. just pass sprURL.getUrl()

Google Appscript approvals for public document

I have a google spreadsheet that is shared with all members on our domain. I'd like to have a script which will send the spreadsheet as an email attachment whenever one of the users runs the script.
Following this question as a guide, I arrived at the following code:
function sendEmail() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
//var email = Session.getUser().getEmail();
var email = "xxxxx#example.com";
var subject = "Order Form";
var body = "Please find the attached Order Form for drop-ship delivery.";
var oauthConfig = UrlFetchApp.addOAuthService("google");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
var requestData = {"method": "GET", "oAuthServiceName": "google", "oAuthUseToken": "always"};
var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="
+ ssID + "&gid=0&portrait=true" +"&exportFormat=xls";
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
MailApp.sendEmail(email,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
}
This code works, but only if I run the code from the script editor first (which involves authorizing access to the google mail account), and then authorizing the script when using the script.
So, I passed along the document to the order department, and again, it only works when EACH USER authorizes the script from the script editor, and then authorizes the script when using it.
Is there a way to eliminate the "authorizing by way of the script editor"? I'd really not like to have to go into each users account to authorize this script for them (as well as have to remember to do the same for any new user created)?
I appreciate any help offered!
Unfortunately NOT. see also my question and the answer by one of the google developer advocates.
You can vote for the issue I opened to push for a solution.

Google Forms Complex Email Send on Form Submit with Script

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.