title: Call another function from within same script file
body: <p>On this post, Serge replies: "but you can, of course, use any function from different script files inside a project".</p>
  
  <p>What is the protocol to do that?</p>
  
  <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/19283684/how-to-make-a-call-from-one-google-apps-script-to-a-function-in-another">How to make a call from one Google Apps Script to a function in another?</a></p>
  
  <p>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.</p>
  
  <p>Thanks!</p>
  
  <p>UPDATED AFTER MOGSDAD RESPONSE:</p>
  
  <p>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:</p>
  
  <pre><code>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 += '&lt;a href='+url+'&gt;Here's the new document&lt;/a&gt;' //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);
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>***EDIT AFTER SERGE'S COMMENT</p>
  
  <p>Serge, here's the code in the original .gs file</p>
  
  <pre><code>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"));
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>Thanks again!</p>