I'm using this Google Apps script to extract and parse data fields from the body text of gmail messages. I've modified the script so that it works for the fields I have in my e-mails. However, I've discovered that it only works when e-mails have different subject lines. For example, if I have five e-mails all with different subject lines, the script will create 5 rows in a Google spreadsheet, one for each e-mail. But if I have 5 e-mails all with the same subject line, I only get one row for the oldest email in the inbox. Does anyone know how I can modify this script so that the subject line can be the same for all e-mails? Thanks!
/* Based on https://gist.github.com/Ferrari/9678772 */
function parseEmailMessages(start) {
start = start || 0;
var threads = GmailApp.getInboxThreads(start, 100);
var sheet = SpreadsheetApp.getActiveSheet();
for (var i = 0; i < threads.length; i++) {
// Get the first email message of a threads
var tmp,
message = threads[i].getMessages()[0],
subject = message.getSubject(),
content = message.getPlainBody();
// Get the plain text body of the email message
// You may also use getRawContent() for parsing HTML
// Implement Parsing rules using regular expressions
if (content) {
tmp = content.match(/Name:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/Title:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var title = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/Organization:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var organization = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/City:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var city = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/State:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var state = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/country:\s*([A-Za-z0-9.\s]+)(\r?\n)/);
var country = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/E-mail:\s*([\s\S]+)/);
var email = (tmp && tmp[1]) ? tmp[1] : 'No comment';
sheet.appendRow([username, title, organization, city, state, country, email]);
} // End if
} // End for loop
}