How can I send image attachment to email - excel

As I have created list of qr codes to scan and then I want to send this images to email by using Google app script.
current result
image is not sent to gmail.
codes
function sendEmail(sheetName = "emails") {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).activate();
var spreedsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = spreedsheet.getLastRow();
//var ssa = SpreadSheetApp().getSheetByName(sheetName); // spread sheet object
const messageBody = "Dear receiver, We would like to invite you on upcoming seminar please use this link {actual link} and show receiptionis this bar code {actual code}"
const subjectText = "Seminar invitation"
// loop rows or spread sheet
for (var i=2; i<= lastRow; i++) {
var receiverName = spreedsheet.getRange(i, 1).getValue();
var receiverEmail = spreedsheet.getRange(i, 2).getValue();
var seminarLink = spreedsheet.getRange(i, 3).getValue();
var barcode = spreedsheet.getRange(i, 4).getBlob();
var msgBdy = messageBody.replace("receiver",receiverName).replace("{actual link}", seminarLink);
var photoBlob = barcode
MailApp.sendEmail({
to: receiverEmail,
subject: subjectText,
htmlBody: messageBody + "<br /><img src='cid:qrCode'>",
inlineImages: { qrCode: photoBlob }
});
}
}

From your following reply,
images are created from =IMAGE("chart.googleapis.com/…) with that google sheet excel
In this case, I thought that your goal can be achieved using Google Apps Script.
When I saw your script, getValue() is used in a loop. In this case, the process cost will become high. In this modification, this issue is also modified.
Modified script:
function sendEmail(sheetName = "emails") {
var spreedsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var lastRow = spreedsheet.getLastRow();
const messageBody = "Dear receiver, We would like to invite you on upcoming seminar please use this link {actual link} and show receiptionis this bar code {actual code}";
const subjectText = "Seminar invitation";
const range = spreedsheet.getRange("A2:D" + lastRow);
const values = range.getValues();
const formulas = range.getFormulas();
values.forEach(([receiverName, receiverEmail, seminarLink], i) => {
const url = formulas[i][3].split('"')[1];
const photoBlob = UrlFetchApp.fetch(url).getBlob();
var msgBdy = messageBody.replace("receiver", receiverName).replace("{actual link}", seminarLink);
MailApp.sendEmail({
to: receiverEmail,
subject: subjectText,
htmlBody: messageBody + "<br /><img src='cid:qrCode'>",
inlineImages: { qrCode: photoBlob }
});
});
}
When this script is run, the values are retrieved from "A2:D" of the sheet. And, each value is retrieved from each row. In the case of the column "D", the URL is retrieved from the formula, and the image blob is retrieved using UrlFetchApp. And, those values are put as the email.
References
forEach()
fetch(url, params)

Update 2
As said in the comments, replace this line:
var barcode = spreedsheet.getRange(i, 4).getBlob();
To:
var barcode = UrlFetchApp.fetch(spreedsheet.getRange(i, 4).getFormula().match(/\"(.*?)\"/)[1]).getAs('image/png')
Update
As you are inserting the image via a IMAGE Formula, you can easily parse the value, obtain the URL and get the Blob via UrlFetchApp
Sample:
const sS = SpreadsheetApp.getActive()
function testSendImage() {
const img = sS.getRange('A1').getFormula()
const urlImg = img.match(/\"(.*?)\"/)[1]
const fetchImage = UrlFetchApp.fetch(urlImg).getAs('image/png')
MailApp.sendEmail({
to: "some#gmail.com",
subject: "QR",
htmlBody: 'Check this QR <img src="cid:fetchImage" />',
inlineImages: { fetchImage: fetchImage } })
}
In the current state, there is no method for extrancting the image from a cell, however there is a Feature Request on Google's Issue Tracker, you can click here to review it.
Remember to click in the top right if you want this feature to be implemented.
In any case, you review this StackOverflow question for workarounds or alternative methods.

Related

How to get Values from Yahoo Finance XHR's Complex Object in Google Apps Script

I'm trying to pull historical quarterly revenue data from Yahoo Finance. Its default "Annual" webpage is updated by selecting "Quarterly" option button, but its URL is still same. It appears that "Quarterly" option only updates the screen behind. So I was not able to pull quarterly data from the URL, but annual data only. Tracking of its XHR request, I figured out its source header, by the following codes below. Now I face another challenge on how to extract quarterlyTotalRevenue from its complex object, because of my lack of understanding about object handling. What I finally want to get is an array, something like this.
How can I do that? Thank you for any help!
function test() {
var ticker = 'JPM';
var url = 'https://query1.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/'
+ ticker + '?lang=en-US&region=US&symbol=' + ticker + '&padTimeSeries=true&type=' + '%2cquarterlyTotalRevenue' +
'%2c&merge=false&period1=493590046&period2=1672016399&corsDomain=finance.yahoo.com';
var obj = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
var obj = JSON.parse(obj); //Convert strings to object
console.log(obj);
}
When your script is modified, how about the following modification?
Modified script:
var obj = ### // Please set your value.
var valeus = [["asOfDate", "raw"], ...obj.timeseries.result[0].quarterlyTotalRevenue.map(({ asOfDate, reportedValue: { raw } }) => [asOfDate, raw])];
var res = valeus[0].map((_, c) => valeus.map(r => r[c]));
// return res; // If you want to use this as a custom function, please use this.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, res.length, 2).setValues(res);
In this modification, the values are put to the active sheet. If you want to use this script as a custom function, please return the values like return res;.
Reference:
map()

Gmail to Google Spread Sheet (only date, email and subject)

The code I have cobbled together does work, but it imports the wrong things from my email. I only want the date sent, the sender email address and the subject to import into the google sheet.
Can anyone help?
function onOpen() {
const spreadsheet = SpreadsheetApp.getActive();
let menuItems = [
{name: 'Gather emails', functionName: 'gather'},
];
spreadsheet.addMenu('SP LEGALS', menuItems);
}
function gather() {
let messages = getGmail();
let curSheet = SpreadsheetApp.getActive();
messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
}
function getGmail() {
const query = "to:legals#salisburypost.com";
let threads = GmailApp.search(query,0,10);
let messages = [];
threads.forEach(thread => {
messages.push(thread.getMessages()[0].getPlainBody());
label.addToThread(thread);
});
return messages;
}
function parseEmail(message){
let parsed = message.replace(/,/g,'')
.replace(/\n*.+:/g,',')
.replace(/^,/,'')
.replace(/\n/g,'')
.split(',');
let result = [0,1,2,3,4,6].map(index => parsed[index]);
return result;
}
I believe your goal as follows.
You want to retrieve "the date, sender and subject" from the 1st message in the searched threads to the active sheet of Google Spreadsheet.
For this, how about this answer?
Modification points:
In this case, you can retrieve "the date, sender and subject" using the built-in methods for GmailApp.
When the values are put to the Spreadsheet, when appendRow is used in the loop, the process cost will become high.
It seems that label is not declared in your script.
When above points are reflected to your script, it becomes as follows.
Modified script:
In this modification, I modified gather() and getGmail() for achieving your goal.
function gather() {
let messages = getGmail();
if (messages.length > 0) {
let curSheet = SpreadsheetApp.getActiveSheet();
curSheet.getRange(curSheet.getLastRow() + 1, 1, messages.length, messages[0].length).setValues(messages);
}
}
function getGmail() {
const query = "to:legals#salisburypost.com";
let threads = GmailApp.search(query,0,10);
let messages = [];
threads.forEach(thread => {
const m = thread.getMessages()[0];
messages.push([m.getDate(), m.getFrom(), m.getSubject()]);
// label.addToThread(thread);
});
return messages;
}
When no threads are retrieved with "to:legals#salisburypost.com", the values are not put to the Spreadsheet. Please be careful this.
References:
getDate()
getFrom()
getSubject()
setValues(values)

Attach excel workbook or convert sheets into excel, then send via gmail using gscript, so that workbook is editable by recipient?

I want to be able to send an email to 100+ emails that I have, with an attachment. The attachment needs to be an editable excel file for the recipients. I can access it either as excel or sheets, whichever is easier.
I've tried as much as I could find already online. And I can send a PDF, and I can send a Google Drive link which delivers an editable sheets doc; but I want the end user to be able to access/edit it even if they do not have Google Drive.
This one successfully sends an editable PDF:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B2
var dataRange = sheet.getRange(startRow, 1, numRows, 1);
// 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'
var file = DriveApp.getFileById("xxxxxxxxx");
MailApp.sendEmail(emailAddress, subject, message, {attachments: [file]});
}
}
I tried this one, to be able to attach a document (hoping an editable excel file) but I get an error code saying that the document is missing (even though it's the exact same doc ID I use in the above, which works):
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B2
var dataRange = sheet.getRange(startRow, 1, numRows, 1);
// 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'
var file = DocumentApp.openById("xxxxxxxxx");
MailApp.sendEmail(emailAddress, subject, message, {attachments: [file]});
}
}
I wanted it to send an excel file, but it won't even find the document even when I use the same doc ID that I use in the 1st syntax above, which works.
This sends an excel doc.. but recipient unable to open. Is this not formatting correctly?
function getGoogleSpreadsheetAsExcel(){
try {
/*var ss = SpreadsheetApp.getActive();*/
var ss = DriveApp.getFileById("xxxxx");
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?
key=" + ss + "&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("xxxx", "Google Sheet to Excel", "The
XLSX file is attached", {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
}
Here's the correct syntax to send email from gmail with an excel attachment using google script
function excelemail(){
var file = DriveApp.getFileById('1pJpDI2HD28_gPWGnj-emV0L4rXBBS0HC');
GmailApp.sendEmail('xxxx', 'Attachment example', 'Please see the attached file.' , {attachments: [file], name: 'Automatic Emailer Script' })
}

sending Email with Attachment xpages

this is my sample coding for sending email with attachment. The content of word is not send correctly.
attachment is using "file upload"
// write mail
var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("MailDBSvr");
var dname = setdoc.getItemValueString("MailDbPath");
var web = setdoc.getItemValueString("InternetAddress");
var maildoc:NotesDocument = database.createDocument();
maildoc.replaceItemValue("Form", "Memo");
maildoc.replaceItemValue("Subject","Test Send Mail");
session.setConvertMime(false);
var stream = session.createStream();
stream.writeText("<html><body>");
stream.writeText("<p>Dear " + "[person]" + ",</p>");
stream.writeText("<p>Attached item is an image of </p>");
stream.writeText("<p> ***THIS IS AN AUTOMATED MESSAGE - PLEASE DO NOT REPLY DIRECTLY TO THIS EMAIL***</p>");
stream.writeText("</body></html>");
var body = maildoc.createMIMEEntity("Body");
var Att= document1.getDocument(true).getFirstItem("Attachment");
maildoc.copyItem(Att,"Body") // try adding an item
body.setContentFromText(stream, "text/html;charset=UTF-8", 1725);
stream.close();
maildoc.closeMIMEEntities(true);
session.setConvertMime(true);
maildoc.replaceItemValue("SendTo","TestUser1#devsvr1.pcs.com.my");
maildoc.send();
The result come out is only the attachment field without any text value inside. I not sure which part of it is wrong.
sample Result screen:
Recommended Mime style
var HTMLMail = function() {
...
}
var mail = new HTMLMail();
mail.setTo("TestUser1#devsvr1.pcs.com.my")
//mail.addFileAttachment(result);
mail.setSubject("Test Send Mail");
mail.addHTML("<h1>Hi!</h1>");
mail.addHTML("<table><tbody><tr><td>contents in a table here</td></tr></tbody></table>");
mail.send();
IBM Notes/Domino has 2 ways to show 'pretty words and pictures'
RichText
MIME
You should only use one or the other, but here you are actually mixing both the different types.
Above, when you copy the Attachment Item, you are actually adding the first 'Body' item, you can see it's type 'RichText'.
Then we you createMimeEntity you are creating the second 'Body' item, and it's type is 'MimePart' (it is probably showing second because the Mime is not applied until CloseMimeEntities)
So now you have 2 Body items with different parts. You are seeing the 'RichText' attachment in Notes because it is first listed item.
What you actually need to do is create the correct multipart mime structure.
If you want a bit more information about mime, I a blog post on my site which explains it a little bit more, including some info about the correct mime structure.
http://camerongregor.com/2016/04/21/webmail-ui-you-must-learn-about-mime/
If you haven't seen it yet there is an XSnippet by Mark Leusink which has a demo of creating a mime email using SSJS. I don't use this myself as I don't use SSJS but it might be useful to you as it should handle most of this mime manipulation for you.
https://openntf.org/XSnippets.nsf/snippet.xsp?id=create-html-mails-in-ssjs-using-mime
My coding with reference using rich-text style :
var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("MailDBSvr");
var dname = setdoc.getItemValueString("MailDbPath");
var web = setdoc.getItemValueString("InternetAddress");
var maildoc:NotesDocument = database.createDocument();
maildoc.replaceItemValue("Form", "Memo");
maildoc.replaceItemValue("Subject","Test Send Mail");
session.setConvertMime(false);
var stream = session.createStream();
stream.writeText("<html><body>");
stream.writeText("<p>Dear <b>" + "person" + "</b>,</p>");
stream.writeText("<p> ***THIS IS AN AUTOMATED MESSAGE - PLEASE DO NOT REPLY DIRECTLY TO THIS EMAIL***</p>");
stream.writeText("</body></html>");
var tmpDoc:NotesDocument = maildoc.getParentDatabase().createDocument();
var mime:NotesMIMEEntity = tmpDoc.createMIMEEntity("myBody");
var addRt:NotesMIMEEntity = maildoc.getMIMEEntity("addBody");
var Att:NotesRichTextItem = document1.getDocument(true).getFirstItem("Attachment");
if(addRt != null && #Length(addRt.getContentAsText().trim()) > 28) {
stream.writeText('<font size="2" face="sans-serif">'); // Enforce "simiilar" font type/size...
stream.writeText(addRt.getContentAsText());
stream.writeText('</font>');
}
mime.setContentFromText(stream, "text/html", NotesMIMEEntity.ENC_NONE);
var prevMime = session.isConvertMime();
session.setConvertMime(true);
tmpDoc.closeMIMEEntities(true,"myBody");
var rt:NotesRichTextItem = maildoc.getFirstItem("Body");
var body = null;
if (rt != null) {
body = rt.copyItemToDocument(tmpDoc,"Body");
rt.remove();
}
rt = maildoc.createRichTextItem("Body");
var rtMime:NotesRichTextItem = tmpDoc.getFirstItem("myBody");
rt.appendRTItem(rtMime);
if(Att != null) {
if(addRt == null) rt.addNewLine(1);
rt.appendRTItem(Att);
Att.remove();
}
if(body != null) {
rt.addNewLine(2);
rt.appendRTItem(body);
}
if(addRt != null) {
addRt.remove();
addRt.recycle();
}
stream.close();
maildoc.closeMIMEEntities(true);
session.setConvertMime(true);
maildoc.replaceItemValue("SendTo","TestUser1#devsvr1.pcs.com.my");
maildoc.send();

activation of customer by sending link to his email using Suitelet Scripts

Overview:
I create one custom customer form using Suitelet Script with fields like name,email,phone in GET Method and the values entered in this fields obtained in the POST Method by request.getParameter() method. the customer record is created is successfully but when clicking the submit button the customer record is to be "inactive" mode and the url link is to sent to the customer email address ,when he clicks the link his record is to be changed to"active" mode this must be with in same suitelet page.
My Requirement:
I created the customer record successfully with INACTIVE MODE and link this send the customer mail but i need how to make the customer record to ACTIVE MODE when he clicks the link and the result is to be viewed in same suitelet page.
I have Given my code Sample Below:
function getCustomerInfo(request, response){
if(request.getMethod() == 'GET'){
//CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
var form = nlapiCreateForm('Custom Customer Form');
form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
form.addField('sfg_address1','text','Address1');
form.addField('sfg_address2','text','Address2');
form.addField('sfg_city','text','City');
form.addField('sfg_state','text','State');
form.addField('sfg_emailaddr','email','Email').setMandatory(true);
form.addField('sfg_phone','phone','Phone');
form.addSubmitButton('Submit');
response.writePage(form);
}else{
nlapiLogExecution('DEBUG','form',form);
// CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
var compName = request.getParameter('sfg_company');
var compAdd1 = request.getParameter('sfg_address1');
var compAdd2 = request.getParameter('sfg_address2');
var cities = request.getParameter('sfg_city');
var stateName =request.getParameter('sfg_state');
var email_addr = request.getParameter('sfg_emailaddr');
var phone_num = request.getParameter('sfg_phone');
var newCust = nlapiCreateRecord('customer');
newCust.setFieldValue('companyname', compName);
newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
newCust.setLineItemValue('addressbook', 'city', 1, cities);
newCust.setLineItemValue('addressbook', 'state', 1, stateName);
newCust.setFieldValue('email', email_addr);
newCust.setFieldValue('phone', phone_num);
newCust.setFieldValue('subsidiary', 1);
newCust.setFieldValue('isinactive','T');
//sending activation link to the customer
var sender = nlapiGetUser();
var receiver = email_addr;
var subject = 'Customer Activation Link';
var recordId = nlapiGetRecordId();
var url = "https://system.na1.netsuite.com/app/common/entity/custjob.nl?id="+recordId+"&whence=";
var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
nlapiSendEmail(sender,receiver,subject,body);
var id = nlapiSubmitRecord(newCust);
nlapiSetRedirectURL('RECORD','customer',id,null,false);
}
}
It sounds like what you basically want is to send out a confirmation link to a customer that they can click and complete some sort of registration.
If you want to re-use your suitele code then you need to pass a custom stage or action parameter that you can trigger different actions from. e.g.:
var stage = request.getParameter('custparam_action') || 'showForm';
switch(stage){
case 'showForm': doShowForm(request, response); break;
case 'validate' : doValidate(request, response); break;
}...
Then the url you need to send out needs to reference the suitelet rather than the customer page in Netsuite:
var ctx = nlapiGetContext();
var url = nlapiResolveURL('SUITELET', ctx.getScriptId(), ctx.getDeploymentId(), true) +"&custparam_action=validate"+ getSecureValidationParams());
the function getSecureValidationParams should create some sort of time sensitive parameters including a hashed key. If the request passes validation then you would show whatever other form you want to show or redirect to or show a thank you page.
function getCustomerInfo(request, response){
if(request.getParameter('custscript_sfg_custmer_param') != null){
var value = request.getParameter('custscript_sfg_custmer_param')
var loadRecord = nlapiLoadRecord('customer',value);
loadRecord.setFieldValue('isinactive','F');
loadRecord.setFieldValue('custentity_sfg_referredby',1);
nlapiSubmitRecord(loadRecord);
nlapiSetRedirectURL('RECORD','customer',value);
}
if(request.getParameter('custscript_sfg_custmer_param') == null){
if(request.getMethod() == 'GET'){
//CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
var form = nlapiCreateForm('Custom Customer Form');
form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
form.addField('sfg_address1','text','Address1');
form.addField('sfg_address2','text','Address2');
form.addField('sfg_city','text','City');
form.addField('sfg_state','text','State');
form.addField('sfg_emailaddr','email','Email').setMandatory(true);
form.addField('sfg_phone','phone','Phone');
form.addSubmitButton('Submit');
response.writePage(form);
}
}
if(request.getMethod() == 'POST'){
nlapiLogExecution('DEBUG','form',form);
// CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
var compName = request.getParameter('sfg_company');
var compAdd1 = request.getParameter('sfg_address1');
var compAdd2 = request.getParameter('sfg_address2');
var cities = request.getParameter('sfg_city');
var stateName =request.getParameter('sfg_state');
var email_addr = request.getParameter('sfg_emailaddr');
var phone_num = request.getParameter('sfg_phone');
var newCust = nlapiCreateRecord('customer');
newCust.setFieldValue('companyname', compName);
newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
newCust.setLineItemValue('addressbook', 'city', 1, cities);
newCust.setLineItemValue('addressbook', 'state', 1, stateName);
newCust.setFieldValue('email', email_addr);
newCust.setFieldValue('phone', phone_num);
newCust.setFieldValue('subsidiary', 1);
newCust.setFieldValue('isinactive','T');
//sending activation link to the customer
var sender = nlapiGetUser();
var receiver = email_addr;
var subject = 'Customer Activation Link';
var recordId = nlapiGetRecordId();
var webAddress = "https://system.na1.netsuite.com"
var scriptType = nlapiGetContext().getScriptId();
var scriptId = nlapiGetContext().getDeploymentId();
var location = nlapiResolveURL('SUITELET',scriptType,scriptId);
var id = nlapiSubmitRecord(newCust);
var link = webAddress+location+'&custscript_sfg_custmer_param='+id+"&whence=";
var url = 'Click Here ';
var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
nlapiSendEmail(sender,receiver,subject,body);
nlapiSetRedirectURL('SUITELET',scriptType,scriptId);
}
The above code will Get Customer Information through external Suitelet form and saving the record in the system.

Resources