Is there a way to "include" another script or access it's functions?

8,022 views
Skip to first unread message

Allaire Jean-Francois

unread,
Aug 30, 2017, 5:08:51 PM8/30/17
to AdWords Scripts Forum
I mean apart from Apps Scripts execution (which seems to make a call over the Internet for every execution).

I have searched but found nothing on this topic.

My best case would to use multiple scripts in the same scope, like in "Google Apps Script".

I must not be the only one who sees a benefit in this

I'm pretty sure the answer is "no", just checking...

Thanks in advance.

Thea Vega (AdWords Scripts Team)

unread,
Aug 31, 2017, 3:12:43 AM8/31/17
to AdWords Scripts Forum
Hi Allaire,

Aside from Apps Scripts Execution that you mentioned earlier, there is another way of accessing methods. This is by using the External Data Integration feature in AdWords Scripts. It is done by storing the script in Google drive, then accessing the said script on your current script in your AdWords account. Below is a sample script to get you started:

function main() {
 
var scriptName = "INSERT_SCRIPT_NAME_HERE";
 
var scriptText = getScript(scriptName);
 
 
if (scriptText) {
   
Logger.log("Running script: "+scriptName);
   
eval(scriptText);
   
var script = eval('new '+externalScript+'();');
   
//function call for external methods
    script
.main();
    script
.nextFunction();
   
//insert other script logic here
 
}
}

function getScript(scriptName) {
 
var fileIterator = DriveApp.getFilesByName(scriptName);
 
if (fileIterator.hasNext()) {
   
var fileCheck = fileIterator.next();
   
if (fileIterator.hasNext()) {
     
Logger.log("Error: There is a duplicate file with name "+scriptName+".");
   
}
   
return file.getBlob().getDataAsString();
 
}
 
else {
   
Logger.log(scriptName+" not found.");
   
return;
 
}
}

For your external script that is saved from your Drive, below is a sample:

function externalScript() {
 
this.main = function() {
   
//Code here will be executed
   
Logger.log('Main function.');
 
}
 
 
this.nextFunction = function() {
   
//Code here will be executed
   
Logger.log('Next function.');
 
}
}

Let me know if this helps.

Thanks,
Thea
AdWords Scripts Team

Allaire Jean-Francois

unread,
Sep 18, 2017, 2:56:22 PM9/18/17
to AdWords Scripts Forum
I think this is the best solution I've seen... we still have to code these functions every time we want to refer to external scripts but I don't think we can work around that.

Thank you very much for both your answer and the sample code.

Allaire Jean-Francois

unread,
Sep 18, 2017, 3:21:31 PM9/18/17
to AdWords Scripts Forum
I would have one question:
Just to make sure I understand this well...
In the "getScript" function, there is the following line
return file.getBlob().getDataAsString();

would it be "fileCheck" instead of "file" (which doesn't seem to be declared)?

Thanx in advance

Thea Vega (AdWords Scripts Team)

unread,
Sep 19, 2017, 1:32:59 AM9/19/17
to AdWords Scripts Forum
Hi Allaire,

Apologies as I didn't notice the return statement. You are correct, it should be fileCheck instead of file. Thank you for pointing it out.

Regards,
Thea
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages