Jul 29, 2019 05:09 AM
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” ?
Jan 07, 2022 09:31 AM
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()
Jan 07, 2022 12:54 PM
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(parts[0]) * qty
newItems.push(`${newQty} ${parts.slice(1).join(" ")}`)
}
output.set('Full Amount', newItems.join("\n"));
Jan 07, 2022 12:55 PM
And the input variables: