Skip to main content

Can Airtable's own "Export CSV" be triggered via a script?


Show first post

27 replies

kuovonne wrote:

You cannot trigger the existing “Download CSV” feature in Airtable with a script. However, it is possible to use scripting (either in scripting app, or with an automation scripting action) to convert the existing data to CSV format and save that data to a website. The exact details would depend on where you want to save the data.


Hello! I'd love to know how to do that if you're willing to share

Thanks!


  • New Participant
  • 4 replies
  • September 23, 2024
kuovonne wrote:

You cannot trigger the existing “Download CSV” feature in Airtable with a script. However, it is possible to use scripting (either in scripting app, or with an automation scripting action) to convert the existing data to CSV format and save that data to a website. The exact details would depend on where you want to save the data.


Hi Kuovonne,

How would one use scripting to convert the existing data into a csv format? I tried with the below, but couldn't get the URL.createObjectURL to work with Airtable.

 


@kuovonne wrote:

You cannot trigger the existing “Download CSV” feature in Airtable with a script. However, it is possible to use scripting (either in scripting app, or with an automation scripting action) to convert the existing data to CSV format and save that data to a website. The exact details would depend on where you want to save the data.


// Define the input variables
let table = base.getTable("Content Brief"); // Table name
let view = table.getView("Excel Author Flyer Download View"); // View name

// Fetch records from the specified view
let result = await view.selectRecordsAsync();
let records = result.records;

// Check if there are records to process
if (records.length === 0) {
console.log("No records found in the view.");
return;
}

// Convert records to CSV format
function recordsToCSV(records, fields) {
if (records.length === 0) return '';

// Map field names from the result fields
const headers = fields.map(field => field.name);
const csvRows = [];

// Add header row
csvRows.push(headers.join(','));

// Add data rows
for (let record of records) {
const values = headers.map(header => {
const value = record.getCellValue(header);
return `"${String(value || '').replace(/"/g, '""')}"`; // Escape quotes
});
csvRows.push(values.join(','));
}

return csvRows.join('\n');
}

// Fetch the field names for the table/view
const fields = table.fields;
const csvContent = recordsToCSV(records, fields);

// Check if the CSV content is empty
if (!csvContent) {
console.log("CSV content is empty.");
return;
}

// Create a file blob from the CSV content
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = URL.createObjectURL(blob);

// Output the download link
output.markdown(`Download your CSV file [here](${url}).`);

// Clean up the URL object after use
setTimeout(() => {
URL.revokeObjectURL(url);
}, 1000);




Reply