I'm trying to put together a picker in google sheets.
Once a file has been uploaded to google drive, I want the url to be posted in the current cell in the spreadsheet.
This is my pickerCallback located in an html file:
var message;
function pickerCallback(data) {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var id = 'https://drive.google.com/open?id=' + doc[google.picker.Document.ID];
google.script.run.accessSpreadsheet();
} message = id;
document.getElementById('result').innerHTML = message;
}
This returns the url in the picker dialog box.
My accessSpreadsheet function looks like this and is located in a google script file:
function accessSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentCell = sheet.getCurrentCell();
currentCell.setValue(message);
return spreadsheet;
}
The function runs but it cannot define message. Is there a way to access the variable defined in the function in the html file from the google scripts function? Or is there another better way to do this?
Solution:
Pass the string id from client side to server.
Snippets:
google.script.run.accessSpreadsheet(id);
function accessSpreadsheet(id) {
currentCell.setValue(id);
Reference:
Client-Server communication
Related
I'm trying to add an image into the body of the emails. I've been able to get everything else working fine besides the image part. The images I'm trying to use are in the "M Column".
What do I need to add to get the value from the "M Column" and put it into the body of the emails?
-code is provided below
Thanks in advance. Everything I've tried has failed so I just removed everything I attempted and went to what I originally had before I decided to add images to the emails.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 15;
var dataRange = sheet.getRange(startRow, 1, numRows, 15);
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[4];
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail(emailAddress, subject, message);
}
}
[error message][1]
[1]: https://i.stack.imgur.com/nwAJG.png
If you want to send an email which contains images, you could use one of the following options:
If your images are stored as urls in your Spreadsheet
The following code snippet is used to create the attachment containing the image gathered from a url stored in your Spreadsheet.
var imageUrl = sheet.getRange("YOUR_RANGE_FOR_THE_IMAGE").getValue();
var imageBody = "<img src='cid:myImage'>";
var imageBlob = UrlFetchApp.fetch(imageUrl)
.getBlob()
.setName("imageBlob");
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message +"<br/><br/>"+ imageBody,
inlineImages:{
myImage: imageBlob
}
});
The above code snippet uses InlineImages in order to attach the image as an attachment into the email you want to send.
If your images are stored in drive and you have their file ids
The following code snippet is used to create the attachment containing the image gathered from the id of the file which is stored in Drive.
var imageId = sheet.getRange("YOUR_RANGE_FOR_THE_IMAGE").getValue();
var image = DriveApp.getFileById(imageId);
var imageBlob = Utilities.newBlob('', 'image/jpeg');
var message = row[10];
var subject = "Subject Test";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
body: message,
attachments: [image.getAs(MimeType.JPEG), imageBlob]
});
If your images are stored inside a cell and/or as overgrid images
Unfortunately, Google Sheets doesn't have the capacity to return the blob of an image, only a reference to the image which means that you cannot retrieve the images and attach them afterwards in an e-mail using Google Apps Script.
In this case, an alternative would be to use the =IMAGE() function to insert the images in a cell and afterwards you can retrieve them by using the Sheets API V4.
Reference
MailApp Class Apps Script;
Sheets Class Apps Script getRange;
DrivApp Class Apps Script getFileById;
Apps Script Reference;
Sheets API.
I have a trigger set up in Google Sheets so a URL is automatically opened in a new browser window. This works if the URL is hardcoded. I want the URL to be a variable. How do I pass the URL variable from Apps Script to HTML script? I'm a novice coder so please explain like I'm 5.
This function works if the URL is text like 'https://www.google.com'
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('https://www.google.com', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
This function does not if the URL is a variable (I checked the maplink value and its a valid URL)
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('maplink', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
You can join strings in the following manner:
...
var url = 'https://www.google.com';
var js = "<script>window.open('" + url + "', '_blank', 'width=800, height=600');google.script.host.close();</script>";
...
Basically
you close the first part of your hardcoded string by closing the quotes
add the dynamical variabl with +
continue the hardcoded string by appending the second part with + and opening the quotes again
I hope this is clear!
Good afternoon,
I'm new to google script and would like to know how I can get data from an HTML form and put it in a document. I need to put the data into the cube in the title of a file that is created at the click of a button. Thank you.
The following project might be helpful for your problem.
https://github.com/terrywbrady/PlainTextCSV_GoogleAppsScript
In this code, a Google App Script project is deployed as a web app.
The doGet() method returns an html page with a form.
function doGet() {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
return html;
}
The html page has a submit function that invokes the following function.
function handleFormPost(formObject) {
jQuery("input:button").attr("disabled",true);
google.script.run.withSuccessHandler(updateOutput)
.withFailureHandler(fail).doTextPost(formObject);
}
This function calls the doTextPost() method in the App Script code.
function doTextPost(req) {
var name = getParam(req, "name", "");
var folderid = getParam(req, "folderid", "");
var delim = getParam(req, "delim", ",");
var resp = createPlainTextSpreadsheet(req.data, name, folderid, delim);
return JSON.stringify(resp);
}
The success handler defined on the html page processes the JSON data returned from this function.
function updateOutput(data) {
var resp = jQuery.parseJSON(data);
document.getElementById("output").innerHTML="<a href='"+resp.url+"'>"+resp.name+"</a> created on Google Drive";
}
i want to get the meta description of the parent page from an iframe, what i did uptill now is that i get the url of the parent page, pass that url to jquery and try to get the meta description but it doesn't work, my code is as follows
<script type="text/javascript">
function addToInterest() {
var URL = parent.window.location;
var Title = parent.document.getElementsByTagName("title")[0].innerHTML;
var MetaDescription = "";
var Img_Src = "";
var metaDesc = $.get('http://myURL.com', function (data) {
MetaDescription = $(data).find('meta[name=description]').attr("content");
Img_Src = $(data).find('link[rel=image_src]').attr("href");
});
alert(MetaDescription);
alert(Img_Src);
}
</script>
But in both alerts, it shows nothing.. i have already tried the methods told here
but did not successfull.
any sample code please....
Regards:
Mudassir
$.get is asynchronous. Both your alerts executed just after $.get call, but at this moment HTTP request can be still in progress. You need to move your alerts inside of callback function:
<script type="text/javascript">
function addToInterest() {
var URL = parent.window.location;
var Title = parent.document.getElementsByTagName("title")[0].innerHTML;
var MetaDescription = "";
var Img_Src = "";
var metaDesc = $.get('http://myURL.com', function (data) {
MetaDescription = $(data).find('meta[name=description]').attr("content");
Img_Src = $(data).find('link[rel=image_src]').attr("href");
alert(MetaDescription);
alert(Img_Src);
});
}
</script>
Also note, what your code will hit Same Origin Policy. By default you can't dynamically load resources, placed on other host, than you script.
Afternoon All,
I need to be able to embed images into an outlook email via a html form that runs locally on a workstation desktop. There is no internet access. All machines are Windows with Outlook 2007.
The below code works great for creating emails with a HTML body, but I also need to embed images into the email that are stored on the local documents folder of the user.
Is there a way to find out what directory path is to the users documents folder?
And how can I embed the images so they show within the body content NOT as an attachment?
<script type="text/javascript">
function OpenOutlookDoc()
{
try
{
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject="a subject test";
mailItem.To = document.getElementById("name").value;
mailItem.HTMLBody = document.getElementById("name").value + " " + "<b>bold</b>";
mailItem.display (0);
}
catch(e)
{
alert(e);
// act on any error that you get
}
}
</script>
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject="a subject test";
mailItem.To = document.getElementById("name").value;
mailItem.Attachments.Add("C:\pictest.jpg");
mailItem.HTMLBody = "<html><p>This is a picture.</p><img src='cid:pictest.jpg' height=480 width=360>";
mailItem.display (0);