Skip to content

Instantly share code, notes, and snippets.

@jonbow4
Last active June 28, 2024 12:15
Show Gist options
  • Save jonbow4/1141b3081795d6aa7b0445895156a27e to your computer and use it in GitHub Desktop.
Save jonbow4/1141b3081795d6aa7b0445895156a27e to your computer and use it in GitHub Desktop.
let table = base.getTable('People');
let csvFile = await input.fileAsync(
'Pick a CSV file to upload',
{allowedFileTypes: ['.csv'], hasHeaderRow: true}
);
if(csvFile) {
let fileRows = csvFile.parsedContents;
output.text('Here are the first 10 records of your file');
output.table(fileRows.slice(0, 10));
let proceed = await input.buttonsAsync(
'Would you like to continue with this import?',
[{label: 'Proceed', variant: 'primary'}, 'Cancel']
)
if(proceed == 'Proceed') {
let newRecords = fileRows.map(fileRow => ({
fields: {
'First Name': fileRow.first_name,
'Last Name': fileRow.last_name,
'Email': fileRow.email
}
}));
output.text('Importing data');
while (newRecords.length > 0) {
await table.createRecordsAsync(newRecords.slice(0, 50));
newRecords = newRecords.slice(50);
}
output.text('Import completed');
}
}
@mateusico
Copy link

Hello! Great code!
Do you know how I could hard code the file? (I don´t want to select it all the time).
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment