Help

Remove unused select field options easily!

1476 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Lom_Labs
7 - App Architect
7 - App Architect

This extension will help you find unused select dropdown data (from either single select or multiple select fields) and delete them!

Remove unused select field options.gif

Base with extension installed

To use it, install the "Script Extension" and paste the code below into it, and a video of how to do that is below

Install.gif

 

Code:

let settings = input.config({
    title: `Identify and remove select field options that are not in use`,
    items: [
        input.config.table("table", { label: `Table` }),
        input.config.field("field", { parentTable: `table`, label: `Field` }),
    ],
});

let { table, field } = settings;

let fieldData = table.getField(field.id)
let fieldType = fieldData.type
if(fieldType != "singleSelect" && fieldType != "multipleSelects" ) throw("Only works with single select or multiple select fields")

let query = await table.selectRecordsAsync({fields: [field]})
let inUse = new Object;
for (let r of query.records){
    let selectedOptions = r.getCellValue(field)
    if(selectedOptions){
        if(fieldType === "multipleSelects"){
            for(let option of selectedOptions){
                inUse[option.name] = 1
            }
        }
        else{
            inUse[selectedOptions.name] = 1
        }
    }
}

let updatedOptions = new Array
let notInUse = new String

for(let option of field.options.choices){
    if(inUse[option.name]){
        updatedOptions.push(option)
    }
    else{
        notInUse = notInUse + "\n - " + option.name
    }
}
if(notInUse === ""){
    output.text("All select field options are in use")
}
else{
    output.text("The following options are not in use:" + notInUse)
    let proceed = await input.buttonsAsync('Proceed with deletion of the listed options? *CAUTION: This cannot be undone.*', ['Delete', 'Do nothing']);

    if (proceed === "Delete") {
        output.text('Deleting..');
        await field.updateOptionsAsync(
            {choices: updatedOptions},
            {enableSelectFieldChoiceDeletion: true},
        )
        output.text('Done');
    } else {
        output.text('Nothing deleted.  Ending.');
    }
}

 

1 Reply 1

Nice! This is an extremely useful script! 😎 If you get a chance, would you mind sharing this with my community at TableForums.com?