Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Dec 28, 2022 03:15 PM - edited Dec 28, 2022 03:21 PM
Hello everybody,
I have a table that's thousands of lines long, with a column called "Formula" which contains small string expressions like:
amount = salary * 0.11;
What I'm trying to do is run this string in my code, so that it updates the `amount` variable based on which line I select. However, I am running into an error when I try to run the string as code.
This is my code, which is able to compile with the correct answer in other online compilers. However, when I run this in my automation script (specifically the line where I call `func()`) I receive TypeError: func is not a function.
let code = "amount = salary * 0.11;";
// Define the function with the desired arguments
let func = new Function('salary', code);
// Call the function with the desired argument values
func(50000);
console.log(amount);
Dec 28, 2022 03:30 PM
eval() is not available in automation scripts. This was a specific choice that Airtable made when implementing automation scripts.
Dec 28, 2022 03:33 PM
Hi Kuovonne,
Thanks for your quick reply. I understand this, and this is why I’m attempting to use a function constructor instead to accomplish the same thing. Is this explicitly forbidden, like using eval() is? I feel like it shouldn’t be
Dec 28, 2022 03:44 PM
I understand this, and this is why I’m attempting to use a function constructor instead to accomplish the same thing. Is this explicitly forbidden, like using eval() is? I feel like it shouldn’t be
It's the same concept. Airtable doesn't like automation scripts to run code generated at run-time. If you feel that the behavior should be different, you can submit a feature request to Airtable support.
Dec 28, 2022 03:46 PM
If you do not need the code to run automatically, you can run your code from a button in Scripting Extension. Scripting extension does not have the same limitations as automation scripts.
Dec 28, 2022 03:48 PM
Unfortunately, it needs to run automatically. Is there ways to automate button presses using the scripting extension, or something you know of that accomplishes something similar? None of these calculations are super complicated on their face, but I’d love to avoid the alternative of creating some sort of language that needs to be parsed by JavaScript on a much lower level. I appreciate the guidance