Skip to main content

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: m
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: n
{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: n
{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, t{name: keyName, type: keyType},{name: fieldName, type: "singleLineText"}]);
}

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

There aren’t enough memes in these forums.

I’m going to change that.



Neat idea! Thanks for sharing.


Hi,

just want to add that when you create a new table by ‘new linked field’ menu



, it creates a table with 1 record and 2 fields


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]=S'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)=>fnamestix].split(', ').map(fname=>(`{name:'${fname}',type:'${t}'}`)))
const command=`base.createTableAsync('${tname}',t${mflds.join()]}])`
await table.updateRecordAsync(rec,{nCOMM]:command})

Hi,

just want to add that when you create a new table by ‘new linked field’ menu



, it creates a table with 1 record and 2 fields


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]=S'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)=>fnamestix].split(', ').map(fname=>(`{name:'${fname}',type:'${t}'}`)))
const command=`base.createTableAsync('${tname}',t${mflds.join()]}])`
await table.updateRecordAsync(rec,{nCOMM]: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 nTNAME,FNAMES,FTYPES,COMM]=M'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=S...new Set(base.tables.flatMap(t=>t.fields.map(f=>f.type)))]
const items=e] //<<to get all filetypes in choices use =JSON.stringify(typSet.map(t=>({'name':t})))
const empty=p'singleLineText','multilineText'].map(name=>&name,''])
const numeric=ri'number','{precision:0}'],}'currency','{precision:0,"symbol":"$"}']]
const select=e'multipleSelects','singleSelect'].map(name=>&name,`{choices:${items}}`])
const opt=(Object.fromEntries(i...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)=>fnamesaix].split(', ').map(fname=>convert(fname,t)))
const command=`await base.createTableAsync('${tname}',e${,flds.join()]}])`
await table.updateRecordAsync(rec,{eCOMM]:command})

Reply