I have been searching all over the internet for a way to export a database table in google app maker by email in HTML format. Ideally, I'm looking for a simple solution like a button that you can click and confirm. It doesn't have to be good looking just has to work.
Best, Grant
You will have to format the HTML table yourself and then use the MailApp.sendEmail()
function using the htmlBody
parameter.
It is possible to write some generic function which accepts an array of records and a header array (or a model object) and generates a HTML table. I have not written such a function but below is a quick and dirty example for generating an HTML table and sending through email.
You would ideally have some event attached to an export button which passes widget.datasource.items
to that HTML formatting function.
// Array of records to send
var records = [
{name:'Foo', owner:'Bob', date: new Date()},
{name:'Bar', owner:'Max', date: new Date()}
];
// Instantiate table string
var table = '<table cellspacing="0" border="1" cellpadding="15" width="400">';
// Add header
table += '<tr><th>Name</th><th>Owner</th><th>Date</th>';
for(var i in records){
var record = records[i];
table += '<tr><td>' + record.name + '</td><td>' +
record.owner + '</td><td>' +
record.date.toLocaleString() + '</td></tr>';
}
table += '</table>';
// Send via MailApp service
var message = {
to: 'me@gmail.com',
subject: 'Your HTML Table',
htmlBody: table,
body: 'Sorry, your mail client does not support HTML emails.'
};
MailApp.sendEmail(message);
And the output:
As an aside, if you have pagination enabled on your datasource your function will only include that many records. Either disable pagination or load the records server side using var records = app.models.myModel.newQuery().run()
. You can also filter the returned records by way of normal query filters.