Sep 19, 2021 11:54 AM
Hi there,
I’m very new to js,
Is there a way to fill a column with data from a row with a script linked to a button?
Thanks!
Sep 20, 2021 05:19 PM
Without getting into the nitty gritty of matching field type to field type, the most basic javascript to copy one field value from the record on which a button was clicked into another field in all records:
let config = input.config({
title: 'Copy one value from one record into all records',
description: 'Test script developed by Kamille Parks, provided without warranty, to answer the following question from the Community Forums: https://community.airtable.com/t/auto-fill-a-column-with-data-from-the-same-row/43196',
items: [
input.config.table('table', {
label: 'Table'
}),
input.config.field('originField', {
label: 'Origin Field',
description: 'Field from which a value will be copied',
parentTable: 'table'
}),
input.config.field('destinationField', {
label: 'Destination Field',
description: 'Field into which the value will be pasted',
parentTable: 'table'
}),
]
})
let {table, originField, destinationField} = config
let query = await table.selectRecordsAsync({fields: [destinationField, originField]})
let records = query.records
let record = await input.recordAsync('Selected Record', table)
if (record) {
let originValue = record.getCellValueAsString(originField)
let updates = records.map(x => {
return {
id: x.id,
fields: {
[destinationField.id]: originValue
}
}
})
while (updates.length > 0) {
await table.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
output.markdown("*Done!*")
} else {
output.text("No record selected")
}