Lukáš Mešťan

Software engineer with a background in full-stack Web development & Open Source Enthusiast.


Adwords scripts multi-file support

- 2 mins

On July 18, 2018 google adwords teams announce the launch of a new feature called “Multi-file script support”. The new feature is in the new AdWords (google ads) interface, there are clearly “files” of code within each “Script”. Default file is Code.gs (you are not limited to a single Code.gs file). You can spread server code across multiple files for ease of development.

adwords multiple files

All of the server files are loaded into the same global namespace, so you should use classes or functions when you want to provide safe encapsulation. This functionality lets you separate your utility logic from your business logic, organize your code and generally produce more maintainable scripts.

Simply said:

You don’t need to do anything, every function in every script files in the same project is accessible from any script file…,
the separation in script files is only a comfortable way to store things the way you want. The project is the only “real” container.

Some useful hints about “main” function

  1. The main function is required
  2. Script must have only one main function
  3. The main function can be in any file, but only once

Useful examples

I would like to provide a small example, how to use multiple file feature. You can create Log.gs file on left side in editor and paste “log” functions.

adwords log.gs file

Log.gs file:

function log(s) {
    Logger.log("[LOG]: " + s);
}

function notice(s) {
    Logger.log("[NOTICE]: " + s);
}

function info(s) {
    Logger.log("[INFO]: " + s);
}

function warning(s) {
    Logger.log("[WARNING]: " + s);
}

function error(s) {
    throw new Error(s);
}

In the main Code.gs file you can use our log functions.

adwords code.gs file

Code.gs file:

function main() {
    log('log message');
    notice('notice message');
    info('info message');
    warning('warning message');
    error('error message');
}

After running your script you can see result in LOGS output:

adwords logs script