Case: I receive notifications through email for work with evaluations of the personal security system. This is what they look like (I blacked out some pieces because of privacy laws) enter image description here
I want to extract some information from this email to google sheets through Google Apps Script (javascript language)
So far I have only managed to extract the date from the email into Google sheets, with this code:
function onOpen(e){
ui.createMenu("Tessa").addItem("Get Emails by Label", "getGmailEmails").addToUi(); //create button in google sheets, which will be used to add information from incoming emails to Google sheets.
}
function getGmailEmails(){
var label = GmailApp.getUserLabelByName("tobeprocessed"); //get emails with label 'tobeprocessed' (could also be another name).
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++){
var message = messages[j];
if (message.isUnread()){
extractDetails(message);
GmailApp.markMessageRead(message);
}
}
threads[i].removeLabel(label); //delete label after processing the message, so that it won't be added twice (or more if you press the button again)
}
}
function extractDetails(message){
var emailData = {
Locatie: "Null",
Score: "Null",
Datum: "Null",
Opmerking: "Null",
}
var emailKeywords = {
Locatie: "",
Score: " ",
Opmerking: "",
}
emailData.Datum = message.getDate();
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var emailDataArr = [];
for(var propName in emailData){
emailDataArr.push(emailData[propName]);
}
activeSheet.appendRow(emailDataArr);
Can someone please help me out with this? Thanks in advance!