Help

Re: Get first Letter of words to create acronym

3558 3
cancel
Showing results for 
Search instead for 
Did you mean: 
De_La_Campa
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello,

I consult and deal with a lot of companies who only use their acronyms…

I am creating a light CRM, and would like to Input the org name into a field, and have a script that returns the acronym for the name in another field.

I have found it very hard to do in Airtable without a Zapier integration, I wonder if the scripting block can help solve these problems.

Any help and code would be greatly appreciated!! Thank you.

8 Replies 8
Nigel_James
5 - Automation Enthusiast
5 - Automation Enthusiast

This should do the trick:
This splits a phrase at spaces, returns another array of the capitalised first letters and then joins them back together with no separator.

    "Today I Learned".split(" ").map(word => word[0].toUpperCase()).join('');

This is just freakin’ clever - thanks!

De_La_Campa
5 - Automation Enthusiast
5 - Automation Enthusiast

thank you for the help!

I am able to create an acronym from a string, however, how can I get this to split the record I am referencing for all the records?

let OrgTable = base.getTable(“Organizations”);

let orgName = OrgTable.getField(“Organization”);

let queryResult = await OrgTable.selectRecordsAsync();

let record = queryResult.records[0];
?record.split(" ").map(word => word[0].toUpperCase()).join(’’);

I appreciate the support

You’ll want to call split on the org name, not the record itself — try replacing that line with this:

let orgNameForRecord = record.getCellValueAsString(orgName);
let acronymForRecord = orgNameForRecord.split(' ').map(word => word[0].toUpperCase()).join(’’);

Thank you @Stephen_Suen for the final touches!

Here is the complete code put together.

let orgTable = base.getTable("Organizations");

let orgRecords = await orgTable.selectRecordsAsync({

    sorts: [

        // sorts by "Organization" in ascending order

        {field: "Organization"},

    ]

});

for (let record of orgRecords.records) {
    recordValue = record.getCellValueAsString("Organization");
    recordSplit = recordValue.split(" ").map(word => word[0].toUpperCase()).join('');
    orgAcronym = output.markdown(`**${recordSplit}**`);
 
}

Thank you so much for the help… It took me awhile, but as someone who has only taken one python class in college, this was fun and I cant wait to learn more!

The next step for me, would be to split the string, then identify if the first letter is capitalized, then place the capitalized letters together into an acronym.

For instance: right now “Cali Coat for Urban Housing” = CCFUH
I would like to get to “Cali Coat for Urban Housing” = CCUH

is there any support that can be lent for this next iteration? :pray: :pray: :raised_hands:

You can replace the inside of your for loop with this:

let recordValue = record.getCellValueAsString("Organization");
// Instead of using Array.map, we'll build up the acronym string as we go.
let orgAcronym = "";

// For each word in the cell, check if the first letter is uppercase.
// If it is, add it to our acronym string.
for (let word of recordValue.split(" ")) {
    if (word[0] === word[0].toUpperCase() {
        orgAcronym = orgAcronym + word[0];
    }
}
output.markdown(`**${orgAcronym}**`);
De_La_Campa
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello,

It has been awhile and I didnt get to offer my gratitude before all this bit of “New Normal” started happening. I really appreciated the help, and ran into some trouble I cant figure out…

// Markdown for intro
output.markdown('# Hello, world!');

//prompts user to input their name. Returns string using input(name).
let name = await input.textAsync('What is your name?');
output.text(`Welcome to the scripting block, ${name}.`);

// pulls every "Organization"  from the table "Organizations"
let orgTable = base.getTable("Organizations");
// Query the records from the "Organizations" table for use
let orgRecords = await orgTable.selectRecordsAsync({
    sorts: [
        // sorts by "name" in ascending order
        {field: "Organization"},
    ]
});

// for every record in the orgRecords, split @ " " and map the first index (letter) to join without spaces
for (let record of orgRecords.records) {
    let recordValue = record.getCellValueAsString("Organization");
// Instead of using Array.map, we'll build up the acronym string as we go.
    let orgAcronym = "";

// For each word in the cell, check if the first letter is uppercase.
// If it is, add it to our acronym string.
    for (let word of recordValue.split(" ")) {
        if (word[0] === word[0].toUpperCase()) {
        orgAcronym = orgAcronym + word[0];
        }
    }
    
output.markdown(`**${orgAcronym}**`);
    
};

let acronym = "California Coalition for Rural Housing".split(" ").map(word => word[0].toUpperCase()).join('');
output.inspect(acronym)

Causes this error: “TypeError: Cannot read property ‘toUpperCase’ of undefined”

I cant figure out why the method wont work. Thank you for the help! Be well.

It’s simple actually; you are trying to perform a string operation on a value that doesn’t exist (i.e., it is probably a field that is returning a null value, ergo - it is undefined).

To remedy this, you need to be certain you actually have a field value before attempting to perform string methods on it.