Overview
Macro is a reusable fragment of code that can be referenced in the Code Editor. Read more about macro.
This article describes how to pass parameters to the macro.
Passing parameters to macros
There are two distinct approaches to passing parameters to macros.
Using parent scope variables
Initialize variable before macro in your script and then use it directly in the macro itself. For example, if you create a macro that shortens data variable to 7 characters and adds tree dots at the end:
data = data.substring(0,7) + '...';
you can use it in your script this way:
var data = 'Some long text';
{{SomeMacroId:Shorten data}}
then in the end, after the macro is applied your script will look like
var data = 'Some long text';
data = data.substring(0,7) + '...';
Using macros to import functions
A much better approach is to create a function inside of your macro, import it at the beginning of the script and then use it as many times as you want. This will allow you to reuse it in any context without relying on the variable's names. Macro from the previous example would become something like this:
function shortenString(string, length) {
if (string.length > length) {
return string.substring(0, length - 3) + '...';
} else {
return string;
}
}
now you can import it to any of your scripts regardless of variable names
{{SomeMacroId:Shorten string function}}
var data = shortenString('Some long text', 10);
var anotherData = shortenString('Even longer text', 20);
Comments
0 comments
Please sign in to leave a comment.