No more default fields!

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"}]);
}

4 Likes

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

3 Likes

Neat idea! Thanks for sharing.

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.

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})
1 Like

Hey, that is a good trick to know!