Call another function from within same script file
function, google-apps-script
Solution
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 = "xyz@xyz.com"
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
}
Problem
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 = "xyz@xyz.com" 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!