This is the code i have so far.
At the end of the email, I would like to provide social icons like these ones that i found free online. And to lead them to corresponding links i have. Minus the skype.
https://codepen.io/anon/pen/QgjeXw
function sendEmails() {
var allData,counter,emailAddress,fourRowsOfData,i,lastRow,
message,messageStart,messageEnd,numRows,
row,sheet,startRow,studentName,subject;
//USER INPUT
startRow = 2; // First row of data to process
numRows = 4; // Number of rows to process
subject = "Camp Pursuit: Report Card ";
messageStart = "Dear Parents,"+'\n' +'\n' +
"Every week at Camp Pursuit, we ask the teachers to make observations about how the kiddos did." +'\n' +
"We know that one week isn't enough to truly know a child, so we call this our " +"Glows, Grows, & Apropos." +'\n' +
"Meaning..." + '\n' +'\n' +
"Glows: Positive things the teacher noticed" +'\n' +
"Grows: Areas for continued growth" +'\n' +
"Apropos: Ways to continue your son/daughter's enrichment" +'\n' +
"We hope you appreciate seeing what the teachers saw last week, and perhaps you can use some of"+'\n' +
"the recommendations for further enrichment this summer. Feel free to let us know your thoughts on the camp through our anonymous online survey."+'\n' +
"Your feedback helps us improve the experience for all kiddos! "+'\n' +
"Survey Link: https://docs.google.com/forms/d/1g4LvA9a8sdKECr1yvp5uOoPnvKACFm3HvbTBfvQGRyo/viewform?usp=send_form" +'\n' +
"We look forward to seeing your child at our programs throughout the year!" +'\n' +
"Sincerely, "+'\n' +
"The Camp Pursuit Team"
+'\n';
//END USER INPUT
sheet = SpreadsheetApp.getActiveSheet();
lastRow = sheet.getLastRow();
allData = sheet.getRange(startRow, 1, lastRow-startRow, 10);// Get All data first
counter = 0;
while (counter < lastRow-startRow) {
Logger.log('counter: ' + counter)
fourRowsOfData = sheet.getRange(startRow + counter, 1, numRows, 6).getValues();
emailAddress = fourRowsOfData[0][0]; // First column of first row
Logger.log('emailAddress: ' + emailAddress)
studentName = fourRowsOfData[0][1];
messageEnd = "";//Reset on every outer loop
for (i = 0; i < numRows; i++) {//loop numRows times - once for each row
row = fourRowsOfData[i];
messageEnd = messageEnd + '\n' +
"Class: " + row[2] + '\n' +'\n' +
"Glows: " + row[3]+ '\n' +
"Grows: " + row[4] +'\n' +
"Apropos: " + row[5] +'\n';
}
message = messageStart + "\n" + studentName + "\n" + messageEnd;
MailApp.sendEmail(emailAddress, subject, message);//Send the email outside of inner loop
counter+=numRows;//Increment counter by number of rows to process
}
}
I don't think you will be able to use examples from codepen as they involve lots of code (e.g. for displaying hover states, etc). However, you could save the buttons as images and store them in your Drive or elsewhere.
The next step is to get the blob for your image from external URL
var imageBlob = UrlFetchApp.fetch(yourImageUrl).getBlob();
or from Drive
var imageBlob = DriveApp.getFileById(yourFileId).getBlob();
Build the string for the html body of your email. Use 'br' tags for line breaks. Wrap your images in 'a' tags to get them to link to external websites. In 'src' attribute of the 'img' tag, create a unique id for your image blob and put it after 'cid'.
var body = "<h1> Header </h1> <br />" +
"<a href='www.example.com'><img src='cid:uid' /></a> Image <br />" +
"Content";
Instead of string parameters, pass the following object to MailApp.sendEmail() method. At runtime, the script will parse the string stored in 'body' variable and create html output, using cid identifiers to retrieve images from inlineImages object. The property names of 'inlineImages' object must match the ids you put created for your images in 'body'
MailApp.sendEmail({
to: "recipient#example.com",
subject: "subject",
htmlBody: body,
inlineImages: {
uid: imageBlob
}
});
Concatenating the string to produce html body is not the best solution though. Consider creating a reusable html template using HtmlService https://developers.google.com/apps-script/guides/html/
Related
I've hit a roadblock I just can't get past after 5+ hours of fiddling. Basically what I'm trying to do is take responses from my API (a recipe search tool) and stick each individual recipe into its own card, having 2 cards side by side all the way down the page. The responses include various info about the recipe to be placed dynamically into each card, but everything I've tried working off the Doc's has just broken my page completely.
My JS (edited to remove a couple chars from my API key, sorry but it's a trial):
//On Index page this function takes search bar input and gives ten results for recipes
function createRecipe() {
var mealName = $("#recipeInput").val().trim();
$.ajax({
url: "https://api.edamam.com/search?q=" + mealName + "&app_id=70e00e26&app_key=6c683b56a399b435d00ee3100c0ca",
method: "GET"
}).then(function(response) {
for (i = 0; i < response.hits.length; i++) {
var newDiv = $("#recipeReveal");
newDiv.append("<div class='card col-lg-4 col-md-5 col-sm-10' style=''>")
.append("<div class='card-body'>")
.append("<h5>" + response.hits[i].recipe.label + "</h5>")
.append("<img class='card-img-top'src='" + response.hits[i].recipe.image + "' alt='Recipe Picture'></img>")
.append("<p class='card-text'>Full recipe instructions can be found at: <a href='" + response.hits[i].recipe.url + "'>" + response.hits[i].recipe.url + "</a></p>")
.append("</div>")
.append("<ul class='list-group list-group-flush'>");
for (j = 0; j < response.hits[i].recipe.ingredients.length; j++) {
newDiv.append("<li class='list-group-item'>" + response.hits[i].recipe.ingredients[j].text + "</li>");
}
newDiv.append("</ul>")
.append("<div class='card-body'>")
.append("<button id='result" + (i + 1) + "' onclick='saveRecipe(" + (i) + ")'>Save Recipe</button>")
.append("</div></div>");
}
queriedRecipe0 = (response.hits[0].recipe.uri);
queriedRecipe1 = (response.hits[1].recipe.uri);
queriedRecipe2 = (response.hits[2].recipe.uri);
queriedRecipe3 = (response.hits[3].recipe.uri);
queriedRecipe4 = (response.hits[4].recipe.uri);
queriedRecipe5 = (response.hits[5].recipe.uri);
queriedRecipe6 = (response.hits[6].recipe.uri);
queriedRecipe7 = (response.hits[7].recipe.uri);
queriedRecipe8 = (response.hits[8].recipe.uri);
queriedRecipe9 = (response.hits[9].recipe.uri);
})
}
This is posting into a div with the appropriate ID "recipeReveal", i thought I had it working but it turns out I just made one really long container with a tiny little card at the top that you can't even see. The ideal situation would be to have each recipe in its own card, along with img response, URL, ingredient list, button, etc. If I can get them into individual cards I definitely think I can knock out the CSS/ Grid system aspects easily. Just the JQuery that's getting me.
PS - Any resources/ easy to understand documentation on this subject is also appreciated if you don't have the time to really dive into this one! Thank you. `
I'm working on a small project where I am wanting to randomly pull one row of a google sheet, and then use that data in an HTML table.
For now, my solution has been first to use javascript to make a random number, then generate an HTML table from google sheets for just that row using this method. So, I end up with a URL for an HTML table with just header row, and a random row of data, similar to this. Then I just embed that table as an object in my HTML page. This is the gist of it:
< script >
window.onload = function() {
var albumno = Math.random() * 408;
var albumno = Math.round(albumno);
var albumurl = "https://docs.google.com/spreadsheets/d/UNIQUE-DOCUMENT-ID/gviz/tq?tqx=out:html&tq=SELECT%20B%2C%20D%2C%20E%2C%20F%2C%20G%20WHERE%20A%20MATCHES%20%27" + albumno + "%27&gid=1739065700";
document.getElementById("output").innerHTML = "ALBUM NUMBER: " + albumno + ".";
document.getElementById("albumpath").innerHTML = '<object align="middle" data="' + albumurl + '">';
};
</script>
There are two major drawbacks. First, the table cannot be (easily) formatted when embedded as an object. Second, my google sheet is a list that is added to weekly, and therefore I have to manually adjust the limits of the random value generated in my javascript.
Is there a way to do this more effectively in Javascript? Perhaps by scraping the full table, and then randomly selecting a row of data, which can be used in an proper HTML table (i.e. not embedded as an object)? Or maybe there exists a google sheets API that would help me?
UPDATE:
I have managed to write a quick function in Google Apps Script that picks the random row of data. I have figured two ways to output the data, option 1 as an array, or option 2 as HTML code for a table. Now how do I call this function in my HTML page, and make use of these data?
}
function randomalbum() {
//get spreadsheet
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/[DOCID]/edit#gid=1');
var sh = ss.getSheetByName("Pick List");
//find last album
var Fvals = sh.getRange("F:F").getValues();
var Flast = Fvals.filter(String).length;
var colnum = sh.getRange(2, 1, Flast).getValues();
var albumlast = Math.max.apply(null, colnum)-1;
//pick random row (note the row an album is in is 1 more than the album no.)
var rowrand = Math.round(Math.random()*(albumlast+1));
//extract data of interest
var albumrand = rowrand -1;
var pick = sh.getRange(rowrand, 5).getValues();
var artist = sh.getRange(rowrand, 6).getValues();
var title = sh.getRange(rowrand, 7).getValues();
//make array (option 1)
var array = [albumrand, pick, artist, title];
return array;
//make HTML string (option 2)
var HTMLString = " <table style='width:100%'>"
+ "<tr>"
+ "<th>Album No.</th>"
+ "<th>Picked By</th>"
+ "<th>Artist</th>"
+ "<th>Artist</th>"
+ "</tr>"
+ "<tr>"
+ "<td>" + albumrand + "</td>"
+ "<td>" + pick + "</td>"
+ "<td>" + artist + "</td>"
+ "<td>" + title + "</td>"
+ "</tr>"
+"</table>"
HTMLOutput = HtmlService.createHtmlOutput(HTMLString);
return HTMLOutput
}
Using a Google Sheets API without needing to run your own server: I remember following the suggestions in the following article to solve this a while ago.
https://coderwall.com/p/duapqq/use-a-google-spreadsheet-as-your-json-backend
You'll probably need another service to add CORS headers to the Google response because cors.io apparently no longer exists.
If you want to publish a web app out of the HTMLOutput returned by your function, you can do the following:
Rename your function to doGet: if you do this, Apps Script will run this function whenever a user visits the URL of the web app you are about to publish, as you can see here.
Remove the return array; in your code: the keyword return finishes current function, so the code will never return an HTMLOutput, and an array of values is not a valid object to return in this situation.
Publish the script as a web app: in your script editor, select Publish > Deploy as web app. Then select a Project version, choose under whose authorization the app should run and who should have access to it, and click Deploy. A URL will show app as Current web app URL. If you access that URL, you will see the HTML you created.
I hope this is of any help.
I have a chrome extension that, right now, is purely a cosmetic addition to a Counter-Strike forum and matchmaking site. I'm trying to implement some Javascript to show players ranks when looking at the statistics of a match. Currently, it looks like this: statistics page imgur
I'm trying to add a new column to the left of where players' names appear that shows their ranks. Ranks are currently only stored on the players' profile page, so I'm trying to write code that will go to each players' profile (currently hyperlinked to their name), get their rank and display that as text.
I have 0 understanding of Javascript despite trying to learn it many times but this is a heavily requested feature and I'd like to implement it for my users.
sample statistics page
sample profile page
As of a few months ago, the following code worked:
function findRanks(i) {
var allUsers = $(document).find("#body-match-total" + i + " tr");
$.each($(document).find("#body-match-total" + i + " tr"), function(index, value){
var userLink = "https://play.esea.net/users/" + allUsers[index].children[0].children[1].innerHTML
$.get(userLink, function(data) {
var parsed = $('<div/>').append(data);
rank = $(parsed).find("#rankGraph h1").text();
allUsers[index].children[0].children[1].innerHTML += " (" + rank + ") ";
});
});
}
findRanks(1);
findRanks(2)
Was missing a bracket. Thanks to some random guy on ESEA forums for the help.
Here is the functional code for people with the same issue
function findRanks(i) {
var allUsers = $(document).find("#body-match-total" + i + " tr");
$.each($(document).find("#body-match-total" + i + " tr"), function(index, value){
var userLink = "https://play.esea.net/users/" + allUsers[index].children[0].children[1].innerHTML
$.get(userLink, function(data) {
data = data.replace(/<img[^>]*>/g,"");
var parsed = $('<div/>').append(data);
rank = $(parsed).find("#rankGraph h1").text();
allUsers[index].children[0].children[1].innerHTML += " (" + rank + ") ";
});
});
}
findRanks(1);
findRanks(2);
DISCLAIMER: I'm new to Stack Overflow and I'm not a developer.
I work in IT and we are currently trying to take all of our paper/electronic forms and turn them into Google Forms. I am currently trying to write a script (see below) that will send an email to a specific person based on one of the answers. The good news is that the script works (woot!).
The problem that I'm having is that I get 2 - 3 of the exact email when I test it. It's especially bad in with my gmail account.
If the below code looks atrocious, I'm open to suggestions! Thanks in advance for the help!!
function myFunction() {
var form = FormApp.getActiveForm()
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
var work = itemResponses[3];
var problem = itemResponses[2].getResponse();
var location = itemResponses[4].getResponse();
var name = itemResponses[0].getResponse()
var sheetsLink = docs.google.com/sheetid
}
if (work.getResponse() === "Food Services") {
MailApp.sendEmail('mymail#gmail.com', 'Kitchen Maintenance', name + ' has a problem with ' + problem + ' in the kitchen at ' + location + '. More information for this request can be found at: ' + sheetsLink + '.');
} else {
MailApp.sendEmail('mymail#schoolacct', 'Maintenance', name + ' has a problem with ' + problem + ' at ' + location + '. More information for this request can be found at: ' + sheetsLink + '.');
}
}
It might be easier for you to get the form responses saved in a spreadsheet then tie the apps script to the spreadsheet with an on form submit trigger. This will allow you to have a record of all form responses in the spreadsheet and if anything goes wrong, you can always recheck. On the multiple emails sent, the multiple editors is the thing to check.
I'm faced with a trivial but not-quick-to-get thing: I need to set the font of a string as a strike-through style.
I've tried search for similar solution on Stackoverflow but it wasn't much instructive and helpful.
What I do have:
var bookingDetails = "\nЗаезд: " + row[0] + "\nВыезд: " + row[1] + "\nНомер: " + "«" + row[2] + "»" + "\nТип размещения: " + row[3] + "\n" + "\nЦена за ночь: " + row[4] + " руб." + "\nВнесённый депозит: " + row[8] + " руб." + "\n" + "\nИмя и фамилия гостя: " + row[5] + "\nМобильный телефон: " + row[6] + "\nЭлектронная почта: " + row[7] + "\n" + "\nПримечание: " + row[11];
I need to make the content of bookingDetails striked out without changing anything in the sheet itself. I tried bookingDetails.setFontLine("line-through") but it didn't help by showing the error of "TypeError: Cannot find function setFontLine in object...".
Please, help me fix it.
Just letting you know that I'm very thankful for your attempt to help me in advance.
UPD № 1: I'm using Google Spreadsheets. If it does make sense.
UPD № 2: If it is not possible to make this strike-through transformation just in the script not explicitly in cells then I should add that bookingDetails is used in the email body to be sent through MailApp.sendEmail. So if it possible to transform the contents of bookigndbetails directly in the message body then let me know how to make it as quick as possible on the example of my script.
Hoping that know my goal is clear.
Here is a function that will set strikethrough the text
function setStrikethrough(range) {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the current spreadsheet.
var sheet = ss.getSheets()[0]; // Select the first sheet.
var cell = sheet.getRange(range); // Use supplied arguments
cell.setFontLine("line-through"); // set strikethrough
}
You can call this method like this (this will set the line style of the given range to 'line-through'):
// specify the range
setStrikethrough("B2:C4");
See https://developers.google.com/apps-script/reference/spreadsheet/