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.
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.
main
function is requiredmain
functionmain
function can be in any file, but only onceI 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.
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.
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: