Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call another function from within same script file

On this post, Serge replies: "but you can, of course, use any function from different script files inside a project".

What is the protocol to do that?

I have a function that I want to use in three different scripts. It's pretty dense and I'm continually refining it, so I don't want to copy and paste all the code between functions. The files are all within the same project. Serge's response at this link is the closest I can come to confirm it's possible, but it doesn't give the protocol.

How to make a call from one Google Apps Script to a function in another?

To be more specific, I have a function that deletes a file, grabs a template, copies it, renames it, and fills it out with the current data in a spreadsheet. I want to run that function within several emailing scripts that go out at different times, with different messages, to different people.

Thanks!

UPDATED AFTER MOGSDAD RESPONSE:

I have 5 .gs files, all within the same script project (like the two .gs files you showed in your example. One creates a new file (newFile.gs), the others are intended to email it at various times to various audiences. An example of one of the .gs file would look like this:

function emailNewDoc() {

//Here's where I want to run the script newFile.gs so that the new doc is created before the mail goes out

var message = "Hi,";
message += '<a href='+url+'>Here's the new document</a>' //url is defined in newDoc.gs after the new document is created

var subject = "The updated document"
var recipients = "[email protected]"

MailApp.sendEmail(recipients, subject, message);

}

As you can see, I don't know what to type on the second line to get the newFile.gs to run as part of emailNewDoc.gs. Sorry to be pedantic. I'm a total novice, but think google scripts is going to change my life forever if I can figure out the basics.

***EDIT AFTER SERGE'S COMMENT

Serge, here's the code in the original .gs file

function newFile() {

var docTemplate = "[insert template doc key here]"; //this one uses the document key, not the file name like mine did
var copy = DocsList.getFileById(docTemplate).makeCopy("my DOCUMENT");
var url = copy.getUrl();
var copyId = copy.getId();
var copyDoc = DocumentApp.openById(copyId);
var body = copyDoc.getActiveSection();
body.replaceText('{date}', Utilities.formatDate(new Date(sheet.getRange('A1').getValues()), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MMM dd"));
}

As you can see, I define URL, and that's the variable I want to pull into another function so I can email it out.

Thanks again!

like image 725
duck Avatar asked Dec 06 '13 16:12

duck


3 Answers

I think I understand the question you are asking. Here is an example project with its functions to set up my explanation:

scriptOne.gs:

  • functionA();
  • functionB();
  • functionC();

scriptTwo.gs:

  • functionD();
  • functionE();
  • functionF();

In scriptOne.gs, I can call functionD() simply by doing this:

functionA() {
  functionD();
}

functionB() {
}

functionC() {
}

I know it seems super elementary, but I use this simple nomenclature to call functions from the other projects. Does that not work for you? If you could provide a clear example of what you are trying to achieve, I could try and give you a better answer.

If you are trying to run ALL of the functions in scriptTwo.gs in one line, then I would create a runScriptTwo() function in scripTwo.gs that calls all of the functions in your scriptTwo.gs file. Then you can call your runScriptTwo() in my first code, instead of calling functionD(). Does that make sense?

like image 58
cvnntg Avatar answered Nov 15 '22 09:11

cvnntg


Just another answer to clarify your last point :

Here is your code that calls the other function :

function emailNewDoc() {
var url = newFile();  // this is calling the other function and gets the value you need from it  
var message = "Hi,";
message += '<a href='+url+'>Here's the new document</a>' //url is defined in newDoc.gs after the new document is created

var subject = "The updated document"
var recipients = "[email protected]"

MailApp.sendEmail(recipients, subject, 'please read html body',{htmlBody : message} );

}

and in the other function (eventually in another .gs file in the same project change the end of your function) :

function newFile() {
  var docTemplate = "[insert template doc key here]"; //this one uses the document key, not   the file name like mine did
  var copy = DocsList.getFileById(docTemplate).makeCopy("my DOCUMENT");
  var url = copy.getUrl();
  var copyId = copy.getId();
  var copyDoc = DocumentApp.openById(copyId);
  var body = copyDoc.getActiveSection();
  body.replaceText('{date}', Utilities.formatDate(new   Date(sheet.getRange('A1').getValues()), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MMM dd"));
  return url;// this returns the value you need in the other function
}
like image 41
Serge insas Avatar answered Nov 15 '22 08:11

Serge insas


Serge's reply is about script files inside a script project. Script projects can have multiple files. Here's how you create them:

Screenshot

In this example, we have two "gs" files, "Code" and "Utilities":

Screenshot

Any function in the project can invoke the getHtmlTable() function that's in Utilities.gs just by calling it. There's no special protocol required.

The primary other way that you can use functions from other scripts is to create and attach Libraries. Not much to it, once you understand them. Start with the Google Apps Script "Libraries" doc.

like image 36
Mogsdad Avatar answered Nov 15 '22 08:11

Mogsdad