Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Automatically copy one or two from several attachments

Topic Labels: Formulas
543 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Dennis_Erwin
4 - Data Explorer
4 - Data Explorer

Our Attachments field ("Images") contains sometimes five or six images. Is there a formula I can use to copy just, for example, the first or first & second images only?

THANKS!

1 Reply 1

Hi,

You can't do this by formula, 
You can use script for one-time operation
I'm using this code as a common template to copy something from Field1 to Field2 applying some function to copied value.

 

 

const table=base.getTable('Tasks') //set table name here
const [FLD1,FLD2]=['Field1','Field2'] //set field names
const query=await table.selectRecordsAsync({fields:[FLD1]}) //set fields to read data
const example=value=>value.split('').reverse().join('') //sample function - reverse word
const update=r=>({id:r.id,fields:{[FLD2]: example (r.getCellValue(FLD1))}})
const upd=query.records.map(update)
while (upd.length) await table.updateRecordsAsync(upd.splice(0,50))

 

 


You need to:
Set your table name (line 1), set your field names (line 2)
and define the function (line 4, line 5)
you should substitute whole 4th line by your function, and change example  in the line 5 to your function name

attachment cell value is array, or null if empty
so, for one first file you can use such 'arrow-function' (with name 'onefile')
(in JS  "if value!=null then {something} else null" can be written as "value? {something}:null")

 

 

const onefile=value=>value? [value.shift()] : null


so, part of you code should be:

 

 

 

 

...
const query=await table.selectRecordsAsync({fields:[FLD1]}) //set fields to read data
const onefile=value=>value? [value.shift()] : null
const update=r=>({id:r.id,fields:{[FLD2]: onefile (r.getCellValue(FLD1))}})
...​

 

 


for last file use method to get last array element

 

const lastfile=value=>value? [value.pop()] : null

 


and in you want to copy first 2 files:

 

const twofiles=value=>value? value.slice(0,2) : null

 


if you need 2 last files, read how 'slice' works, try to understand and apply using info above. Good luck )
Again, don't forget to change function name in line 5.
Linter will remind you 🙂