Last active
June 28, 2024 12:15
-
-
Save jonbow4/1141b3081795d6aa7b0445895156a27e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! Great code!
Do you know how I could hard code the file? (I don´t want to select it all the time).
Thanks