I have an array, the contents of which are a subset of a list of names that come from a checkbox question in a google form. I need to email the people whose names are in the array, I suppose from a hard coded list (multi-dim array?). I cannot figure out how to perform the search/comparisons/whatever. Apparently I am supposes to use an object literal as in the code below:
var formNames = ["Name One", "Name Three"]; // one possibility for example
var objectMatchingNamesToEmails{
"Name One":"nameone#work.com",
"Name Two":"nametwo#work.com",
"Name Three":"namethree#work.com",
};
You could loop through the array:
var arrayOfEmails,arrayOfNames,L,thisEmail,thisName;
arrayOfNames = ["NameOne","NameTwo"];
arrayOfEmails = [];
L = arrayOfNames.length;//The number of names in the array
for (var i = 0;i<L;i++) {
thisName = arrayOfNames[i];
thisEmail = objectMatchingNamesToEmails[thisName];
arrayOfEmails.push(thisEmail);
};
Create an object literal:
var objectMatchingNamesToEmails;
objectMatchingNamesToEmails = {
"NameOne":"exampleOne#gmail.com",
"NameTwo":"exampleTwo#gmail.com",
"NameThree":"exampleThree#gmail.com",
};
Then after you get the name, the code can look up the correct email:
var userName,userEmail;
userName = code here to get user name;
userEmail = objectMatchingNamesToEmails[userName];
MailApp.sendEmail(userEmail,subject,body);
Related
var String = "One,Three,Eight";
var temp = String.split(",");
var test = (temp[1][0]);
Browser.msgBox(''+test+'', Browser.Buttons.OK_CANCEL);
I've searched high and wide for why this won't return:
"One" as (temp[0][0])
"Three" as (temp[1][0])
"Eight" as (temp[2][0])
They are only returning O, T and E respectively in the message box.
I want each word to be its own array index, is it something to do with a limit?
Thanks
You can test the code by using Logger.log().
function sandbox() {
var String = "One,Three,Eight";
var temp = String.split(",");
Logger.log(temp);
}
This will show you the following value for temp: [One, Three, Eight]
If you log the elements of the array:
temp[0] = 'One'
temp[1] = 'Three'
temp[2] = 'Eight'
If you use temp[1][0] you get the first letter from 'Three'.
Reference:
String.prototype.split()
Accessing array elements
String: Character Access
I am looking for help from this community regarding the below issue.
// I am searching my Gmail inbox for a specific email
function getWeeklyEmail() {
var emailFilter = 'newer_than:7d AND label:inbox AND "Report: Launchpad filter"';
var threads = GmailApp.search(emailFilter, 0, 5);
var messages=[];
threads.forEach(function(threads)
{
messages.push(threads.getMessages()[0]);
});
return messages;
}
// Trying to parse the HTML table contained within the email
function getParsedMsg() {
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var doc = XmlService.parse(msgbody);
var html = doc.getRootElement();
var tables = doc.getDescendants();
var templ = HtmlService.createTemplateFromFile('Messages1');
templ.tables = [];
return templ.evaluate();
}
The debugger crashes when I try to step over the XmlService.parse function. The msgbody of the email contains both text and HTML formatted table. I am getting the following error: TypeError: Cannot read property 'getBody' of undefined (line 19, file "Code")
If I remove the getParsedMsg function and instead just display the content of the email, I get the email body along with the element tags etc in html format.
Workaround
Hi ! The issue you are experiencing is due to (as you previously mentioned) XmlService only recognising canonical XML rather than HTML. One possible workaround to solve this issue is to search in the string you are obtaining with getBody() for your desired tags.
In your case your main issue is var doc = XmlService.parse(msgbody);. To solve it you could iterate through the whole string looking for the table tags you need using Javascript search method. Here is an example piece of code retrieving an email with a single table:
function getWeeklyEmail() {
var emailFilter = 'newer_than:7d AND label:inbox AND "Report: Launchpad filter"';
var threads = GmailApp.search(emailFilter, 0, 5);
var messages=[];
threads.forEach(function(threads)
{
messages.push(threads.getMessages()[0]);
});
return messages;
}
// Trying to parse the HTML table contained within the email
function getParsedMsg() {
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var indexOrigin = msgbody.search('<table');
var indexEnd = msgbody.search('</table');
// Get what is in between those indexes of the string.
// I am adding 8 as it indexEnd only gets the first index of </table
// i.e the one before <
var Table = msgbody.substring(indexOrigin,indexEnd+8);
Logger.log(Table);
}
If you are looking for more than one table in your message, you can change getParsedMsg to the following:
function getParsedMsg() {
// If you are not sure about how many you would be expecting, use an approximate number
var totalTables = 2;
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var indexOrigin = msgbody.indexOf('<table');
var indexEnd = msgbody.indexOf('</table');
var Table = []
for(i=0;i<totalTables;i++){
// go over each stable and store their strings in elements of an array
var start = msgbody.indexOf('<table', (indexOrigin + i))
var end = msgbody.indexOf('</table', (indexEnd + i))
Table.push(msgbody.substring(start,end+8));
}
Logger.log(Table);
}
This will let you store each table in an element of an array. If you want to use these you would just need to retrieve the elements of this array and use them accordingly (for exaple to use them as HTML tables.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)
I'm looking for some help. I am trying to grab an author's publications from PubMed and populate the data into Google Sheets using Apps Script. I've gotten as far as the code below and am now stuck.
Basically, what I have done was first pull all the Pubmed IDs from a particular author whose name comes from the name of the sheet. Then I have tried creating a loop to go through each Pubmed ID JSON summary and pull each field I want. I have been able to pull the pub date. I had set it up with the idea that I would do a loop for each field of that PMID I want, store it in an array, and then return it to my sheet. However, I'm now stuck trying to get the second field - title - and all the subsequent fields (e.g. authors, last author, first author, etc.)
Any help would be greatly appreciated.
function IMPORTPMID(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var author = sheet.getSheetName();
var url = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=" + author + "[author]&retmode=json&retmax=1000");
var response = UrlFetchApp.fetch(url);
var AllAuthorPMID = JSON.parse(response.getContentText());
var xpath = "esearchresult/idlist";
var patharray = xpath.split("/");
for (var i = 0; i < patharray.length; i++) {
AllAuthorPMID = AllAuthorPMID[patharray[i]];
}
var PMID = AllAuthorPMID;
var PDparsearray = [PMID.length];
var titleparsearray = [PMID.length];
for (var x = 0; x < PMID.length; x++) {
var urlsum = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&rettype=abstract&id=" + PMID[x]);
var ressum = UrlFetchApp.fetch(urlsum);
var contentsum = ressum.getContentText();
var jsonsum = JSON.parse(contentsum);
var PDpath = "result/" + PMID[x] + "/pubdate";
var titlepath = "result/" + PMID[x] + "/title";
var PDpatharray = PDpath.split("/");
var titlepatharray = titlepath.split("/");
for (var j = 0; j < PDpatharray.length; j++) {
var jsonsum = jsonsum[PDpatharray[j]];
}
PDparsearray[x] = jsonsum;
}
var tempArr = [];
for (var obj in AllAuthorPMID) {
tempArr.push([obj, AllAuthorPMID[obj], PDparsearray[obj]]);
}
return tempArr;
}
From a PubMed JSON response for a given PubMed ID, you should be able to determine the fieldnames (and paths to them) that you want to include in your summary report. Reading them all is simpler to implement if they are all at the same level, but if some are properties of a sub-field, you can still access them if you give the right path in your setup.
Consider the "source JSON":
[
{ "pubMedId": "1234",
"name": "Jay Sahn",
"publications": [
{ "pubId": "abcd",
"issn": "A1B2C3",
"title": "Dynamic JSON Parsing: A Journey into Madness",
"authors": [
{ "pubMedId": "1234" },
{ "pubMedId": "2345" }
]
},
{ "pubId": "efgh",
...
},
...
],
...
},
...
]
The pubId and issn fields would be at the same level, while the publications and authors would not.
You can retrieve both the pubMedId and publications fields (and others you desire) in the same loop by either 1) hard-coding the field access, or 2) writing code that parses a field path and supplying field paths.
Option 1 is likely to be faster, but much less flexible if you suddenly want to get a new field, since you have to remember how to write the code to access that field, along with where to insert it, etc. God save you if the API changes.
Option 2 is harder to get right, but once right, will (should) work for any field you (properly) specify. Getting a new field is as easy as writing the path to it in the relevant config variable. There are possibly libraries that will do this for you.
To convert the above into spreadsheet rows (one per pubMedId in the outer array, e.g. the IDs you queried their API for), consider this example code:
function foo() {
const sheet = /* get a sheet reference somehow */;
const resp = UrlFetchApp.fetch(...).getContentText();
const data = JSON.parse(resp);
// paths relative to the outermost field, which for the imaginary source is an array of "author" objects
const fields = ['pubMedId', 'name', 'publications/pubId', 'publications/title', 'publications/authors/pubMedId'];
const output = data.map(function (author) {
var row = fields.map(function (f) {
var desiredField = f.split('/').reduce(delve_, author);
return JSON.stringify(desiredField);
});
return row;
});
sheet.getRange(1, 1, output.length, output[0].length).setValues(output);
}
function delve_(parentObj, property, i, fullPath) {
// Dive into the given object to get the path. If the parent is an array, access its elements.
if (parentObj === undefined)
return;
// Simple case: parentObj is an Object, and property exists.
const child = parentObj[property];
if (child)
return child;
// Not a direct property / index, so perhaps a property on an object in an Array.
if (parentObj.constructor === Array)
return collate_(parentObj, fullPath.splice(i));
console.warn({message: "Unhandled case / missing property",
args: {parent: parentObj, prop: property, index: i, pathArray: fullPath}});
return; // property didn't exist, user error.
}
function collate_(arr, fields) {
// Obtain the given property from all elements of the array.
const results = arr.map(function (element) {
return fields.slice().reduce(delve_, element);
});
return results;
}
Executing this yields the following output in Stackdriver:
Obviously you probably want some different (aka real) fields, and probably have other ideas for how to report them, so I leave that portion up to the reader.
Anyone with improvements to the above is welcome to submit a PR.
Recommended Reading:
Array#reduce
Array#map
Array#splice
Array#slice
Internet references on parsing nested JSON. There are a lot.
Linked Google Sheet: https://docs.google.com/spreadsheets/d/1j2P2V0SCiE7QtK7kdCorpLfYQPFRghKMw4tuCYbqce0/edit?usp=sharing
I have a form that outputs all products and quantities into one cell:
"Product: NF900XC, Quantity: 3
Product: NF900, Quantity: 2
Product: N1930CB, Quantity: 2
Product: N2120, Quantity: 1
Product: NLPCR200, Quantity: 2
Product: N272, Quantity: 2"
I need each Product and Quantity on their own Line with its corresponding associated data collection.
I would like the contents (column O) split into their respective columns:
**Product** : **Quantity**
NF900 : 2
N1930CB : 2
N2120 : 1
NLPCR200: 2
N272 : 2
I have used SPLIT(text, delimiter, [split_around_each_character]) command for First Name Last Name but am unsure how to parse the rest of the text into their rows and columns in this situation.
I will also be functionally copying rows with the timestamp in
column A
Submission Date
that correspond to the Products:Quantity in cell O
I hope this makes sense.
Here is some code that I've tested and it works. The code removes all the words "Product: ", and replaces them with a comma. Then the code does something similar with the string "Quantity: ", replacing it with an empty string. Next it creates an array, and converts the array to a two dimensional array, so that the rows and columns can be written all in one action.
function convertData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('Form Submission');
var data = sh.getRange("O2").getValue();
//Logger.log(data);
var firstProductRemoved = data.replace("Product: ", "");
//Logger.log(firstProductRemoved);
var allProductReplacedWithComma = firstProductRemoved.replace(/Product: /g,",");
//Logger.log(allProductReplacedWithComma);
var allQuantityReplacedWithNothing = allProductReplacedWithComma.replace(/Quantity: /g,"");
//Logger.log(allQuantityReplacedWithNothing);
var dataAsArray = allQuantityReplacedWithNothing.split(",");
var outerArray = [], innerArray = [];
var i=0;
for (i=0;i<dataAsArray.length;i+=2) {
innerArray = []; //reset every loop
innerArray.push(dataAsArray[i]);
innerArray.push(dataAsArray[i+1]);
outerArray.push(innerArray);
};
//Logger.log(outerArray);
var orderItemsSh = ss.getSheetByName('Order Items');
orderItemsSh.getRange(orderItemsSh.getLastRow()+1, 15,outerArray.length, 2).setValues(outerArray);
};
You might be interested in a more general solution, rather than one that needs to rely on knowing the specific phrased in the input strings.
This kvStringToArray() function will return an array that can be written directly to a spreadsheet use Range.setvalues(). It's pure JavaScript, so it can also be used outside of the Google Apps Script environment.
A RegExp is used to successively identify key:value pairs in the input data, and the function assumes that there are "rows" of information that can be identified by repeats of already-discovered "keys".
var formInput = "Product: NF900XC, Quantity: 3 Product: NF900, Quantity: 2 Product: N1930CB, Quantity: 2 Product: N2120, Quantity: 1 Product: NLPCR200, Quantity: 2 Product: N272, Quantity: 2";
snippet.log(
JSON.stringify(
kvStringToArray(formInput)));
/**
* Convert a given string of key:value pairs into a two-dimensional
* array, with keys as headers, and "items" as rows.
* From: http://stackoverflow.com/a/34847199/1677912
*
* #param {string} str A string containing key:value pairs.
*
* #returns {string[][]} A two-dimensional array of strings.
*/
function kvStringToArray( str ) {
var re = /\s*(\w*?)\s*:\s*([^,\s]*)/g, // See https://regex101.com/r/kM7gY1/1
arr, // array to capture key:value pairs
data = [], // array to return table
row = [], // array for building table rows
headers = []; // array of unique keys, for table header
// Use a RegEx to identify individual key:value pairs
while ((arr = re.exec(str)) !== null) {
var key = arr[1]; // $1 matches key
var value = arr[2]; // $2 matches value
// Check if we should start a new row
if (headers.indexOf(key) == 0) {
data.push(row);
row = [];
}
// Save this value in row
row.push(value);
// If this is the first time we've seen this key, add it to headers.
if (headers.indexOf(key) == -1) headers.push(key);
}
data.push(row); // save last row
data.unshift(headers); // add headers
return data;
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Is it possible that I could add the body of a gdoc into an email? I kinda have an idea of how to do it but I am not completely sure. I have written this code below to kinda help me. I am new to this and I have managed to have a few scripts running, but I am completely lost on this one. I have watched several videos and this is what I was able to do. The code is below.
Basically what I want to do is to be able to have a user input his name and another variable and then go to the google doc file and change it to the value that was input and then put it back in an email and send it to an address... Any ideas of what I am doing wrong or where should I start?? Thanks in advance.
function gsnot() {
var emailaddress="albdominguez25#gmail.net";
var sub="Subject1";
var pattern = Browser.inputBox("Enter your name");
var pattern2 = Browser.inputBox("Enter the minutes:");
var templateDocID= ScriptProperties.getProperty("EmailTemplateDocId");
var doc = DocumentApp.openById(templateDocID);
var body = doc.getActiveSection()
var html = "";
var keys = {
name: pattern,
min: pattern2,
};
for ( var k in keys ) {
body.replaceText("%" + k + "%", keys[k]);
doc.saveAndClose();
html = getDocAsHtml(docId);
DocsList.getFileById(docId).setTrashed(true);
return html;
var emailaddress="albdominguez25#gmail.net";
var sub="Subject1";
MailApp.sendEmail(emailaddress,sub, {htmlBody: body});}}
You might want to change your code by reading the body from the document into a variable, doing the replace on the variable and inserting that into your email. For example:
function gsnot() {
var emailaddress = "albdominguez25#gmail.net";
var sub = "New Subject";
var pattern = Browser.inputBox("Enter your name:");
var pattern2 = Browser.inputBox("Enter the minutes:");
var templateDocID = ScriptProperties.getProperty("EmailTemplateDocId");
var doc = DocumentApp.openById(templateDocID);
var body = doc.getText();
var replacement;
var k;
var keys = {
name: pattern,
min: pattern2
};
for (k in keys) {
if (keys.hasOwnProperty(k)) {
replacement = new RegExp("%" + k + "%",'g');
body = body.replace(replacement, keys[k]);
}
}
MailApp.sendEmail(emailaddress,sub, '', {htmlBody: body});
}
A few notes:
It is good form to have all your var statements at the beginning of
the function
When you use for-in (eg. for (k in keys) ), it returns all properties of the object. You only want the ones you assigned. This is the reason for: for (k in keys)
You had the mail sending for each property, I believe you wanted it outside the for-in loop so it only sent after all the replacements were completed.
Using replace(), you need to create a regular expression object that is set to global
or it will only replace the first instance of the pattern (you have name twice).
In your parameters for sendEmail(), even if you are using the htmlBody option, you need to specify a plain text body. I used empty quotes ''.