What’s the use of multiple .gs files in Google Ads scripts?

When creating Google Ads scripts, you might have noticed a blue button with a plus sign near the bottom left part of the screen, below Code.gs. Clicking this button will actually allow you to add another script file to your Google Ads script.

 

Using multiple files in Google Ads scripts

According to the Google Ads Scripts Team, each .gs file that’s created within Google Ads has global visibility. This means that if you have a function doSomething() in the Code.gs file, you can create another file called Code2.gs, and call that function doSomething() in Code.gs. 

 

A very handy use case is to use different .gs files to group together helper functions that you use frequently in different scripts. That way, you can simply copy the whole Helper.gs file in your different scripts and have all your helper functions at hand.

It’s also a convenient way to organize more complex Google Ads scripts. Let’s say you have a very long Google Ads script that’s already several hundred lines long. For readability, you can split up the script by storing specific functions in different files. Then you can just call those functions in the main function file. 

This also makes updating more complex scripts easier, as you only need to look for the specific file with the specific function to update it. I for example use a separate file ‘reporting.gs’ to contain the functions used to export reporting data to a Google spreadsheet.

The multiple files feature can also be used to streamline all the scripts that you have in your account. Let’s say you have Script A, Script B, and Script C in your account. Instead of having them stored as separate scripts, you can just have each script stored as functions in separate files. Then similar to the previous method I’ve noted, you can just call each function from the main function file. That way you can limit the number of scripts you need to authorize from you account and circumvent limitations on the number of Google Ads scripts you can run from your account

 

NOTE: Only one main function allowed.

The required “main” function can be added in any of the multiple files you’ve added to Google Ads. However, you can only have one “main” function for all those files.

Here’s an example. Let’s say you’ve added this block of code in your Google Ads script file called File1.gs. Since it already contains the “main” function, you can’t have a file in File2.gs that also has a function called “function main”:

File2.gs:

function main {
  Logger.log("Do the main thing for the second time.");
}

File1.gs  – will cause error with File2.gs, since it’s recursive and the main function appears twice the collectins of .gs files:

function main {
  Logger.log("Do the main for the first time.");
  main();
}

Hence, your function in File2.gs should have a different name–so you can call that function in File1.gs:

File2.gs:

function secondMain {
  Logger.log("Do the main thing for the second time.");
}

File1.gs :

function main {
  Logger.log("Do the main for the first time.");
  secondMain();
}

 

Join thousands of PPC geeks who already have access:

If the button above isn’t working for you, you can sign up here to get access.