Downloading a CSV File from Web-direct

Exporting a file from FileMaker can be accomplished from 3 steps.

1.Export Records[]

2.Export Field Contents[]

3.Save Records as Excel[]

When we need to export data which comes from different tables ,we usually takes the help of Temporary table or we can take the data in HTML form and push it to a field and then Export Field Contents[] ,which is an appropriate way to export data without taking the help of Temporary table.

But Export Field Contents[] works partially in Web-direct.
For that we can invoke a javascript function which can take data in Array or JSON form and can export the data as an Excel/CSV file.

Here is an example to show how to accomplish the task.

Let the data set be: -

Adam,18,M
Phil,20,F
Rick,30,M
Sam,40,F

Then ,we need to convert to JSON form as "String" below by some appropriate custom function :-

var PersonalInfo = [ 
 {
 Name: "Adam",
 Age: 18,
 Gender: "M"
 },
 {
 Name: "Phil",
 Age: 20,
 Gender: "F"
 },
 {
 Name: "Rick",
 Age: 30,
 Gender: "M"
 },
 {
 Name: "Sam",
 Age: 40,
 Gender: "F"
 }
 ];

Then , we need to set a global variable as $$data to below as a web-viewer content :-

Javascript functions adopted from : -https://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/

$$data =


"data:text/html,

<!doctype html> 
 
Export as CSV
 var PersonalInfo = [ 
 {
 Name: \"Adam\",
 Age: 18,
 Gender: \"M\"
 },
 {
 Name: \"Phil\",
 Age: 20,
 Gender: \"F\"
 },
 {
 Name: \"Rick\",
 Age: 30,
 Gender: \"M\"
 },
 {
 Name: \"Sam\",
 Age: 40,
 Gender: \"F\"
 }
 ];

function convertArrayOfObjectsToCSV(args) {
 var result, ctr, keys, columnDelimiter, lineDelimiter, data;

data = args.data || null;
 if (data == null || !data.length) {
 return null;
 }

columnDelimiter = args.columnDelimiter || ',';
 lineDelimiter = args.lineDelimiter || '\n';

keys = Object.keys(data[0]);

result = '';
 result += keys.join(columnDelimiter);
 result += lineDelimiter;

data.forEach(function(item) {
 ctr = 0;
 keys.forEach(function(key) {
 if (ctr > 0) result += columnDelimiter;

result += item[key];
 ctr++;
 });
 result += lineDelimiter;
 });

return result;
 }

function downloadCSV(args) {
 var data, filename, link;

var csv = convertArrayOfObjectsToCSV({
 data: PersonalInfo
 });
 if (csv == null) return;

filename = args.filename || 'export.csv';

if (!csv.match(/^data:text\/csv/i)) {
 csv = 'data:text/csv;charset=utf-8,' + csv;
 }
 data = encodeURI(csv);

link = document.createElement('a');
 link.setAttribute('href', data);
 link.setAttribute('download', filename);
 link.click();
 }
 
 "

Leave a Reply