I have an online form that sends submitted form field data to Gmail. I've been using the script below to extract the form field data into a Google Sheet. Currently, the script extracts from the inbox. I'm wondering if there is a way to adjust the script so that it looks at a Gmail label (for example: formsubmissions) instead of the inbox.
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(/First:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var first = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/Last:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var last = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/Title:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var title = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/Organization:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var organization = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/City:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var city = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/State:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var state = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/Country:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@./\s\-]+)(\r?\n)/);
var country = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/E-mail:\s*([A-Za-z0-9!"?`?|õüö’çëÅíšÃÉÁÇÃáéñãóú#&;()-,'@_.\-]+)/);
var email = (tmp && tmp[1]) ? tmp[1].trim() : 'BLANK';
tmp = content.match(/BLANK:\s*([\s\S]+)/);
var blank = (tmp && tmp[1]) ? tmp[1] : 'BLANK';
sheet.appendRow([first, last, title, organization, city, state, country, email]);
} // End if
} // End for loop
}
threads
from a label of formsubmissions
which is the label name.If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as just one of them.
Please modify as follows.
From:var threads = GmailApp.getInboxThreads(start, 100);
To:
var labelName = "formsubmissions"; // Please set the label name
var labels = GmailApp.getUserLabels();
var threads = [];
for (var i = 0; i < labels.length; i++) {
if (labels[i].getName() == labelName) {
threads = labels[i].getThreads();
break;
}
}
var sheet = SpreadsheetApp.getActiveSheet();
works fine.If I misunderstood your question, I apologize.