Help

No more default fields!

2380 5
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi Airtablers!
This is a silly little thing that I though some others might find useful/funny. I find myself living in my ‘demo’ base most of the time. Users come to me with an Airtable question and I often open my demo base and recreate the issue to help them solve it.

When I do this I often need to make a few tables and fields quickly, yet creating a new table, naming it, renaming the primary field and deleting Notes, Attachments and Status every time is a pain.

So here is a small script to keep at the top of your extensions to quickly create new tables and fields without the need to clean them up after.

const config = input.config({
    title: 'New Table Maker',
    description: 'This script lets you create a new user defined table without the default table options.',
    items: [
        input.config.text('defaultPrefix', {
            label: 'Default New Title Prefix',
        }),
        input.config.select('keySuffix', {
        label: 'Title Suffix',
        description: 'A unique number to append to the Title',
        options: [
            {label: 'Current Date/Time', value: 'curDate'},
            {label: 'Random Number', value: 'ranNum'}
        ]
        }),
        input.config.text('keyName', {
            label: 'Default Title of the Primary Field',
        }),
        input.config.select('keyType', {
            label: 'Type of field for the Primary Field',
            description: 'Select the field type',
            options: [
                {label: 'Single Line Text', value: 'singleLineText'},
                {label: 'Long Text', value: 'multilineText'},
                {label: 'Phone Number', value: 'phoneNumber'},
                {label: 'Email', value: 'email'},
                {label: 'URL', value: 'url'},
                {label: 'Barcode', value: 'barcode'},
            ]
        }),
        input.config.text('fieldName', {
            label: 'Field Name',
        })
    ]
});
const defaultPrefix = config.defaultPrefix;
const keyName = config.keyName;
const fieldName = config.fieldName;
const keyType = config.keyType;
const keySuffix = config.keySuffix;
let fieldString = '{name: "Field", type: singleLineText}';
let createdDate = Date.now();
let randomNumber = Math.floor(Math.random() * 100000);

if(keySuffix == "curDate"){
    const tableId = await base.createTableAsync(defaultPrefix+createdDate, [{name: keyName, type: keyType},{name: fieldName, type: "singleLineText"}]);
}

if(keySuffix == "ranNum"){
    const tableId = await base.createTableAsync(defaultPrefix+randomNumber, [{name: keyName, type: keyType},{name: fieldName, type: "singleLineText"}]);
}

5 Replies 5

There aren’t enough memes in these forums.
I’m going to change that.

young_thug_lil_durk_pc

Neat idea! Thanks for sharing.

______________________________________
Hannah - On2Air.com - Automated Backups for Airtable

Hi,
just want to add that when you create a new table by ‘new linked field’ menu
image
, it creates a table with 1 record and 2 fields
image

Also, I’m trying to create some central tool to manage bases in several workspaces, so I can share my tablecreator script. It requires a table with names and types and doesn’t create a table, just generate a command.

image

const table=base.getTable('TableCreator')
const query=await table.selectRecordsAsync({fields:table.fields})
const [TNAME,FNAMES,FTYPES,COMM]=['Name','Fieldnames','Fieldtypes','Command']
const rec=await input.recordAsync('',table);
if (!rec) throw new Error('record not defined');
const cutter=text=>text.split('"').join('').split(`\n`).join('')
const tname=rec.getCellValue(TNAME);
const fnames=cutter(rec.getCellValue(FNAMES)).split('- ').splice(1);
const ftypes=rec.getCellValue(FTYPES).map(t=>t.name);
if ((fnames.length<ftypes.length)||(!tname)) throw new Error('Names not defined')
const flds=ftypes.flatMap((t,ix)=>fnames[ix].split(', ').map(fname=>(`{name:'${fname}',type:'${t}'}`)))
const command=`base.createTableAsync('${tname}',[${[flds.join()]}])`
await table.updateRecordAsync(rec,{[COMM]:command})

Hey, that is a good trick to know!

Well, if notification returned me here, I’m sharing updated script. Previous didn’t support fields with options.

const table=base.getTable('TableCreator')
const query=await table.selectRecordsAsync({fields:table.fields})
const [TNAME,FNAMES,FTYPES,COMM]=['Name','Fieldnames','Fieldtypes','Command']
const rec=await input.recordAsync('',table);
if (!rec) throw new Error('record not defined');
const cutter=text=>text.split('"').join('').split(`\n`).join('')
const tname=rec.getCellValue(TNAME);
const fnames=cutter(rec.getCellValue(FNAMES)).split('- ').splice(1);
const ftypes=rec.getCellValue(FTYPES).map(t=>t.name);
if ((fnames.length<ftypes.length)||(!tname)) throw new Error('Names not defined')

const typSet=[...new Set(base.tables.flatMap(t=>t.fields.map(f=>f.type)))]
const items=[] //<<to get all filetypes in choices use =JSON.stringify(typSet.map(t=>({'name':t})))
const empty=['singleLineText','multilineText'].map(name=>[name,''])
const numeric=[['number','{precision:0}'],['currency','{precision:0,"symbol":"$"}']]
const select=['multipleSelects','singleSelect'].map(name=>[name,`{choices:${items}}`])
const opt=(Object.fromEntries([...empty,...numeric,...select]))
const option=x=>opt[x]? ', options:'+(opt[x])+'}':'}'
const convert=(name,t)=>`{'name':'${name}','type':'${t}'`+ option(t)

const flds=ftypes.flatMap((t,ix)=>fnames[ix].split(', ').map(fname=>convert(fname,t)))
const command=`await base.createTableAsync('${tname}',[${[flds.join()]}])`
await table.updateRecordAsync(rec,{[COMM]:command})