Last active
February 1, 2019 04:02
-
-
Save kasrak/ed1baa870cd5c3e85ba731839666bb22 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Install lodash and airtable with npm or yarn | |
const _ = require('lodash'); | |
const Airtable = require('airtable'); | |
// Get API key and base ID from https://airtable.com/api | |
const base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('YOUR_BASE_ID'); | |
// Fill in your table and field names. | |
const tableName = 'Table 1'; | |
const emailField = 'Email'; | |
const createdTimeField = 'Created time'; | |
const fieldsToMerge = ['Notes', 'Rating']; | |
base(tableName).select().all().then(async allRecords => { | |
// Sort by created time ascending | |
allRecords = _.sortBy(allRecords, record => record.get(createdTimeField)); | |
// Group by email address | |
const recordsByEmail = _.groupBy(allRecords, record => record.get(emailField)); | |
for (const records of Object.values(recordsByEmail)) { | |
if (records.length === 1) { | |
// No dupes. | |
continue; | |
} | |
// Merge into the oldest record. | |
const cellValuesByField = {}; | |
for (const field of fieldsToMerge) { | |
for (const record of records) { | |
const cellValue = record.get(field); | |
if (cellValue) { | |
// Ignore empty cells. | |
cellValuesByField[field] = cellValue; | |
} | |
} | |
} | |
await records[0].patchUpdate(cellValuesByField); | |
// Delete other records. | |
for (let i = 1; i < records.length; i++) { | |
await records[i].destroy(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment