I have been looking for the answer to this question and so far have found everything around it, but not something that can help me figure out what I am doing wrong. I pull an oracle report every morning that send an email with the report output as a link to a google sheet. I can match every value that I am looking for except for the link value, which matches when I use regex.101. the script I am using is a modified version of the one I have seen in several posts and it works EXCEPT when I try to match the link.
function parseEm(start) {
start = start || 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Results");
var label = sheet.getRange('B2').getValue();
// var threads = GmailApp.getInboxThreads(start, 100);
//var sheet = SpreadsheetApp.getActiveSheet();
var threads = GmailApp.search("Label:" + label, 1, 5)
for (var i = 0; i < threads.length; i++) {
// Get messages in search thread
// Get the plain text body of the email message
var tmp,
message = threads[i].getMessages()[0],
subject = message.getSubject(),
content = message.getPlainBody();
// match each requirement using constant values paired with regex
if (content) {
//Request ID:
tmp = content.match(/Request ID:\s*([A-Za-z0-9\s]+)(\r?\n)/);
var ID = (tmp && tmp[1]) ? tmp[1].trim() : 'No report id';
//Report Name:
tmp = content.match(/Report Name: \s*([A-Za-z0-9@.]+)/);
var Name = (tmp && tmp[1]) ? tmp[1].trim() : 'No report name';
//Report Link :
tmp = content.match(/Report Link: (http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/);
Logger.log(tmp);
var link = (tmp && tmp[1]) ? tmp[1] : 'No link';
Logger.log(link)
//write information to sheet
//sheet.appendRow([ID, Name, subject, link]);
} // End if
} // End for loop
}
The data to match is as follows only modified to take out the report name and details for security purposes. I can pull everything else just fine and it writes to the sheet, but the link returns as No link and Logger returns null.
Request ID: 854632157
Report Name: report Name and Invoice_Number_854632157
Report Link : https://drive.google.com/open?id=id of file stored in drive
I can put the entire email contents into Regex and it matches perfectly as long as I don't add in the required '/' before and after. but as soon as I run the program in apps script, it returns everything but the last value. I just have one request... Even if you think this is a duplicate, please try to answer instead of just writing duplicate... trust me I have looked and I can't figure out what I am doing wrong, and I am pretty sure it is in the last regex parameter, but I can't figure it out.