Skip to main content

Hey there,


Is there a way to multiplyer an array of number to a constant or variable.


I have all my item with a default quantity on a table like so :


I’ve created group of item so I only have to add one group to list of the items with their default quantity (to prevent errors)



My problem is here on the packing List, when I want to be able to multiply the default quantity per Tower I need but I can’t figure out how to do so. How to multiply an array with “Tower needed” ?


Hi

SO how di you multiplied the array values by 2 ?


Hi

SO how di you multiplied the array values by 2 ?


This can’t currently be done with Airtable’s formulas, which have no way of parsing array data. Using Airtable’s native features, the only way to do this is with a script, either in the Scripting app, or via an automation. If you’d like help with either option, let us know.


This can’t currently be done with Airtable’s formulas, which have no way of parsing array data. Using Airtable’s native features, the only way to do this is with a script, either in the Scripting app, or via an automation. If you’d like help with either option, let us know.


Do I need a PRO account with enabling this ?


This can’t currently be done with Airtable’s formulas, which have no way of parsing array data. Using Airtable’s native features, the only way to do this is with a script, either in the Scripting app, or via an automation. If you’d like help with either option, let us know.


DO you have an exmaple of how an automation like this might look like?


Do I need a PRO account with enabling this ?



Use of Airtable’s Scripting app is free for all members until March 2021, at which point it will probably revert to only being available for workspaces that are Pro plan or higher. To use a scripting action in an automation requires the base to be in a Plus plan workspace or higher.



I haven’t yet tried building a script to accomplish what @Carl_Duffaut asked about previously. It probably wouldn’t take long, but I won’t have time to play with it until tomorrow.


Here’s a script that will do the job. This is set up to work in the Scripting app. I don’t recommend using an automation for this because there are several possible changes that would require a recalculation, and catching some of those could be tricky.


// Setup
let table = base.getTable("Group")
let record = await input.recordAsync("Select a record", table)

// Collect the list
let items = record.getCellValueAsString("Item Names").split("\n")
let qty = record.getCellValue("Quantity")
let newItems = s]

// Calculate new totals
for (let item of items) {
let parts = item.split(" ")
let newQty = Number(partsa0]) * qty
newItems.push(`${newQty} ${parts.slice(1).join(" ")}`)
}

await table.updateRecordAsync(record, {"Total Items": newItems.join("\n")})

Here’s the script in action:



Aside from changing the table and field names in the script to match your scenario, the primary requirement is that the items are on separate lines, as the original example above shows. This can be done by using the following aggregation formula for any rollup fields: ARRAYJOIN(values, "\n"). A number must be at the beginning of each line, with a space separating it from later pieces.


Hi Justin! (or other script experts),

I’m hoping to resurrect this thread because I used your code for a similar problem (thank you!). I was able to get it to work as you recommended, and now I’m hoping to modify it so that the script runs through an automation whenever the Quantity field is updated. I pasted the same script into an Automation, which produced an error



I tried to fix that by changing line 3 to read:


let record = input.config()


but this gave me an error on line 6. Any guidance would be appreciated!


Hi Justin! (or other script experts),

I’m hoping to resurrect this thread because I used your code for a similar problem (thank you!). I was able to get it to work as you recommended, and now I’m hoping to modify it so that the script runs through an automation whenever the Quantity field is updated. I pasted the same script into an Automation, which produced an error



I tried to fix that by changing line 3 to read:


let record = input.config()


but this gave me an error on line 6. Any guidance would be appreciated!


Automations run in the background, and a such they can’t get user input. The input object in an automation “Run script” action doesn’t have the same features as the same-named one in the Scripting app. The automation version only contains a single config() method, which is used for retrieving input variables. You’ll need to use that to get the record ID from the trigger step. Pull up the “API” reference at the bottom of the script window, click on “Input”, and you’ll see an example of how to use that method.


Automations run in the background, and a such they can’t get user input. The input object in an automation “Run script” action doesn’t have the same features as the same-named one in the Scripting app. The automation version only contains a single config() method, which is used for retrieving input variables. You’ll need to use that to get the record ID from the trigger step. Pull up the “API” reference at the bottom of the script window, click on “Input”, and you’ll see an example of how to use that method.


Ok so I think I’ve been able to do that. I defined the text that needs to be split as “singleAmount”, then tried to use the same split function. The result was this error:


TypeError: singleAmount.split is not a function


Ok so I think I’ve been able to do that. I defined the text that needs to be split as “singleAmount”, then tried to use the same split function. The result was this error:


TypeError: singleAmount.split is not a function


It sounds like you hit one of Airtable’s idiosyncrasies when it comes to data being fed into an automation script. Long story short, even though there’s only a single record triggering the automation, the other data values—aside from the ID of the triggering record—often get passed as arrays, not single values. Because of this, you’ll need to convert the array to a string first before the split() method will work. One way is to use the same array-to-string conversion method that Airtable functions use: add the array to an empty string, though you’ll need to wrap that in parentheses before you can then use the split() method on it:


(singleAmount + "").split()

Two other options are to use either the join() or toString() array methods, both of which output a string:


singleAmount.join().split()
// ...or...
singleAmount.toString().split()

Thanks very much! That was the info I needed. Here’s the script I ended up with:


// Setup
let table = base.getTable("Production Tracker")
let recordInput = input.config()
let singleAmount = recordInput.singleAmount
// Collect the list
let items = (recordInput.singleAmount + "").split("\n")
let qty = recordInput.quantity
let newItems = =]

// Calculate new totals
for (let item of items) {
let parts = item.split(" ")
let newQty = Number(partst0]) * qty
newItems.push(`${newQty} ${parts.slice(1).join(" ")}`)
}
output.set('Full Amount', newItems.join("\n"));

Thanks very much! That was the info I needed. Here’s the script I ended up with:


// Setup
let table = base.getTable("Production Tracker")
let recordInput = input.config()
let singleAmount = recordInput.singleAmount
// Collect the list
let items = (recordInput.singleAmount + "").split("\n")
let qty = recordInput.quantity
let newItems = =]

// Calculate new totals
for (let item of items) {
let parts = item.split(" ")
let newQty = Number(partst0]) * qty
newItems.push(`${newQty} ${parts.slice(1).join(" ")}`)
}
output.set('Full Amount', newItems.join("\n"));

And the input variables:



Reply