Help

Re: Split the long-text field into different values separated

Solved
Jump to Solution
1638 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Digital_Lion
4 - Data Explorer
4 - Data Explorer

Greetings!

I would love to anybody’s help with solving some not-trivial task for Airtable automation. I have long-text field with many words ( I do not know exact number of words, but all of them separated by " "). Is there any possibility to split this field to different words?

This script for automation. When the form is submitted with long-text field, the script will create as much records as words in long-text field.

Thank you so much

1 Solution

Accepted Solutions

Let’s see if I can do a bit more by first laying out some basic safe practices:

Tip #1: always declare your variables with const, UNLESS you need let.

Tip #2: you only need let if you plan to mutate/change its value; (this includes assigning to unassigned references, like doing let Name; on one line and Name='Digital_Lion' on some other one further down the event loop rabbit hole.

Tip #3: generally speaking, you only want to mutate values if and when the time comes to modify actual table contents. Even then, there are use cases for doing that without mutating your virtual structures representing the desired state of those table contents, but declaring new ones.

#3 probably won’t make much sense right now but try to revisit it after a few more weeks of learning the language…

As for your question:

const texter = 'Young apples lie on the street';
const validator = texter.split(' '); //just the words
const validator2 = text.split(/\b/); //words + whitespace

/* 
 * note how we declared 3 variables here, but mutated zero;
 * because the fewer changes we make to the data we poll, 
 * the fewer opportunities we have to make mistakes;
 * this is important because we make mistakes all the time;
 * e.g. I had to edit the above code twice. TWICE. 
 * Imagine going to teach a kid how to ride a bike but breaking 
 * your foot on step one - that's how my pride feels right now. 😂
*/

console.log(validator);
console.log(validator2);

As you can see, String.prototype.METHOD_NAME would, in this instance, mean: “when you have a reference to a string, invoke the METHOD_NAME method by appending it to the reference itself with a dot.”

You won’t have to worry about the actual mechanics of the prototype keyword for a long time after this. But learning this bit of basic JS notation means you can now read countless other resources online.

If that doesn’t work because e.g. you searched for how to list Object keys in JS because you have an object like this:

const lionObject = {firstName:'Digital',lastName:'Lion',learning:'ECMAScript'}

and the first result says to use the Object.prototype.keys method, the other thing to try is

const lionObjectKeys = Object.keys(lionObject);
console.log(lionObjectKeys);

My terminology is embarrasingly rusty, so maybe someone more up-to-date can chip in but basically, pick a learning resource you like and bear with it for a few hours, you’ll feel like you have Airtable superpowers in no time.

Which brings us to the most important tip of them all:

Don’t torment yourself by trying to figure out JS by ear from reading Airtable docs.

You’ll just get frustrated and quit, Airtable itself links to Mozilla’s docs any chance they get for a very good reason. That reason being that while Airtable’s API docs are mostly a masterclass in technical writing, they are a bad way to learn JavaScript. In the same way an Aston Martin user manual is no resource for learning how to drive and obey traffic laws.

That is to say: Airtable docs don’t exist to teach something as vast as JS; just a very small, exotic use case of JS revolving around custom methods undestood only by Airtable.

Congrats on having the guts to push yourself out of the comfort zone, but the way to keep learning interesting and maybe even fun without it getting too frustrating is by knowing when to use the right tool for the job. Right now, your job is learning the fundamentals more so than splitting this string, the latter part will come in a matter of hours, you just need to be studying the right sources before plunging into Airtable. :slightly_smiling_face:

EDIT: On second thought, I hate the Aston Martin-as-Airtable-surrogate figure. Airtable’s waaay less to go bankrupt before the next Bond movie drops. Again.

See Solution in Thread

10 Replies 10

When the form is submitted with long-text field, the script will create as much records as words in long-text field.

Do you want big and bulky bases overnight? Nevermind, you already got three by evening’s news.

The word-splitting part itself, is trivial, just do a

String.prototype.split(/\b/)

or

String.prototype.split(' ')

call, depending on which one gets you closer to the desired output. But I’d strongly advise rethinking your end goal here as things are likely to get out of hand fairly soon with this approach. Airtables, even on Enterprise plans, can get pretty slow once you’re in the tens of thousands of records.

Thank you so much, my friend. Just tried, but it didn’t work. Please, check my example. What should i do to get “apples” in console?

`let texter = 'Young apples lie on the street';

let validator =  texter.prototype.split(' ');

console.log(`The value of postavka is ${validator}`);`
Digital_Lion
4 - Data Explorer
4 - Data Explorer

Can you show it again, please?

Let’s see if I can do a bit more by first laying out some basic safe practices:

Tip #1: always declare your variables with const, UNLESS you need let.

Tip #2: you only need let if you plan to mutate/change its value; (this includes assigning to unassigned references, like doing let Name; on one line and Name='Digital_Lion' on some other one further down the event loop rabbit hole.

Tip #3: generally speaking, you only want to mutate values if and when the time comes to modify actual table contents. Even then, there are use cases for doing that without mutating your virtual structures representing the desired state of those table contents, but declaring new ones.

#3 probably won’t make much sense right now but try to revisit it after a few more weeks of learning the language…

As for your question:

const texter = 'Young apples lie on the street';
const validator = texter.split(' '); //just the words
const validator2 = text.split(/\b/); //words + whitespace

/* 
 * note how we declared 3 variables here, but mutated zero;
 * because the fewer changes we make to the data we poll, 
 * the fewer opportunities we have to make mistakes;
 * this is important because we make mistakes all the time;
 * e.g. I had to edit the above code twice. TWICE. 
 * Imagine going to teach a kid how to ride a bike but breaking 
 * your foot on step one - that's how my pride feels right now. 😂
*/

console.log(validator);
console.log(validator2);

As you can see, String.prototype.METHOD_NAME would, in this instance, mean: “when you have a reference to a string, invoke the METHOD_NAME method by appending it to the reference itself with a dot.”

You won’t have to worry about the actual mechanics of the prototype keyword for a long time after this. But learning this bit of basic JS notation means you can now read countless other resources online.

If that doesn’t work because e.g. you searched for how to list Object keys in JS because you have an object like this:

const lionObject = {firstName:'Digital',lastName:'Lion',learning:'ECMAScript'}

and the first result says to use the Object.prototype.keys method, the other thing to try is

const lionObjectKeys = Object.keys(lionObject);
console.log(lionObjectKeys);

My terminology is embarrasingly rusty, so maybe someone more up-to-date can chip in but basically, pick a learning resource you like and bear with it for a few hours, you’ll feel like you have Airtable superpowers in no time.

Which brings us to the most important tip of them all:

Don’t torment yourself by trying to figure out JS by ear from reading Airtable docs.

You’ll just get frustrated and quit, Airtable itself links to Mozilla’s docs any chance they get for a very good reason. That reason being that while Airtable’s API docs are mostly a masterclass in technical writing, they are a bad way to learn JavaScript. In the same way an Aston Martin user manual is no resource for learning how to drive and obey traffic laws.

That is to say: Airtable docs don’t exist to teach something as vast as JS; just a very small, exotic use case of JS revolving around custom methods undestood only by Airtable.

Congrats on having the guts to push yourself out of the comfort zone, but the way to keep learning interesting and maybe even fun without it getting too frustrating is by knowing when to use the right tool for the job. Right now, your job is learning the fundamentals more so than splitting this string, the latter part will come in a matter of hours, you just need to be studying the right sources before plunging into Airtable. :slightly_smiling_face:

EDIT: On second thought, I hate the Aston Martin-as-Airtable-surrogate figure. Airtable’s waaay less to go bankrupt before the next Bond movie drops. Again.

Awesome!
Thank you so much!

One more question… So, I converted string to array with split function. How can I make a loop for each array value. Give an example, please or link me to right topic here.

Based on this:

This script for automation. When the form is submitted with long-text field, the script will create as much records as words in long-text field.

You want to build out an an array representing new records like this:

const wordArray = [] //we're still using constant here because the data will stay an array


//assuming the name of the primary/first field
// in your target table is the default 'Name'
validator.forEach(word => wordArray.push({fields:{'Name':word}}));
output.table(wordArray)

while(wordArray.length > 0) //shorthand alternative for this line would be while(!wordArray.length)
await base.getTable('yourTargetTableName') //and we can get away with omitting curly braces on this next statement because it's just the one
    .createRecordsAsync(wordArray.splice(0,50))

Once you start getting the hang of the language syntax (again, Mozilla docs first, then Airtable), the way to discover new Scripting methods/behaviors is by abusing these links I tried underlining at the bottom:

Click for screenshot

image

What makes them great is that they’re full of dynamic examples you can copy and run that already use your base/table/field names and values (i.e. that change with every one of your bases and always just work, as-is). So you can instantly see how everything works.

Hi @Dominik_Bosnjak ,

What a masterpiece of javascript pedagogy for newcomers to the Airtable Scripting App !

At the time Scripting Block Beta was released (12/2019), I was spotting small portions of vanilla javascript that seemed particularly concise and efficient in the scripts of the Community Experts and then I was doing a lot of testing on my own with open MDN on top of the Airtable API Documentation.

But I realised that I was still missing something: learning a coding style like you are teaching here and being able to understand and explain it.
As Google was often directing me to free trial articles from MEDIUM, and after 3 open articles, I was blocked until the end of the month, so I tried to subscribe to MEDIUM.com

If I don’t talk here about Medium Authors who came to promote their new library or their framework which should, according to them, disrupt the world standards occupying these positions, nor about those who come to talk about a language as divine or devilish in their eyes, it’s rather to put the emphasis on those who teach like you : explaining a coding style like the chapter of a course !

That’s how I learned and still learn to not write scripts that just do the job but scripts like you defend here.

It is in Airtable Community that I found concise and efficient ways to script for the Airtable API and it is often in MEDIUM that I found their style explained and justified with a pedagogical concern not mixed or interlaced with Airtable API efficient ways of coding.

Many thanks,

olπ

Because you want an automation script, you will need to remove this line, or change it to console.log(wordArray)


This is a bit tricky to understand when your variable is an array or an object. You need let if you are going to assign the variable a new value. But if you are going to mutate the items in an array or mutate the properties of an object, you do not need to use let, which you demonstrate in your script. The nuances between between changing the variable, versus changing the properties of an object/array can be a bit subtle for a novice coder.


Minor typo: the shorthand would not have the bang (!).


A minor stylistic quibble: even though you do not need the curly braces, I find it helpful if you still indent the line in the loop.


I offer these comments because you seem to be writing to help novice coders, and I thought these few extra tips might help novice coders. Overall, you explanations are great and you have a writing style that is fun to read.

Welcome to the Airtable community!

It sounds like you are new to writing scripts and are very focused on accomplishing your specific tasks. However, if you want to learn to write scripts yourself, please listen to Dominik’s advice to “learn the fundamentals” of JavaScript first.

On the other hand, if you just want working code, but are not actually interested in learning to code, that’s fine as long as you let people know what you want.