You have mainly two ways to add groovy code to your script. The first one is creating functions. The advantage of functions is that you can use them any number of times you wish from your script, so if you want to execute the same code in several places in your script, it's a good habit to write this code inside a function and execute that function everywhere this code should be executed. To see how to create a function take a look to the next section.
The other way of adding groovy code to your script, is directly to put a groovy piece of code in your script to be executed. You can do this using the command 'Execute some code' on the advanced tab in the command panel. If you click on it, a new window with a textbox in it will be opened. You only have to place your code in that textbox and click Ok. Below this textbox you will see a tip panel where the available functions for you to use will be shown. If you type something in the code textbox that functions list will be filtered by what you last typed. It can be useful if you happen to not remember the name of the functions in the api.