I am newbie to google apps script. I am trying to write a google apps script that will take a table from gmail and put the data into google sheets.
The email body is somewhat like the following
CONFIRMATION CODE GUEST'S NAME GUEST'S EMAIL GUEST'S PHONE
A1 Name someone@gmail 012377777
So far I have tried the following the code to get the data in the email and push it to spreadsheet.
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("[Gmail]/test");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getBody();
var sub = messages[j].getSubject();
var dat = messages[j].getDate();
//ss.appendRow([msg, sub, dat])
}
// threads[i].removeLabel(label);
}
}
messages[j].getBody() gives me the following html
<u></u>
<div>
<table cellpadding="3" style="font-size:.9em">
<thead>
<tr>
<th>CONFIRMATION CODE</th>
<th>GUEST'S NAME</th>
<th>GUEST'S EMAIL</th>
<th>GUEST'S PHONE</th>
</tr>
</thead>
<tbody>
<tr>
<td>
A1
</td>
<td>
Name
</td>
<td>
someone@gmail
</td>
<td>
012377777
</td>
</tr>
</tbody>
</table>
</div>
Next I am trying to parse this using the following
var msg = threads[0].getMessages();
var rows = Xml.parse(msg[0].getBody(),true).getElement()
.getElement("div")
.getElement("table")
.getElement("tbody")
.getElements("tr");
But this throwing me the error "TypeError: Cannot call method "getElement" of null. " I do not have much experience with XML so please can you tell me what I am doing wrong here. Thanks :)