Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can One Script Trigger a Function In Another Script?

I need to find a way to have one Google Apps script file trigger a function in another script file.

My goal is to have a master script that copies another script file and creates a new file (this is already done), then triggers a function in the new file. Is this possible?


I am creating a series of fully automated job application scripts and what I need is a master script that creates each of the other scripts. Currently, each job application has a file structure like this:

-Folder --Step 1 (script file for resume upload) --Step 2 (spreadsheet with attached script file for storing step 1 data) --Step 3 (interview scheduling form with an attached script file) --Step 4 (skills test spreadsheet with attached script file) --Step 5 (script file for skills test file upload)


What I need:

-My master script needs to be able to create triggers for unrelated script files 1-5, and I would also prefer it if I could create and append new functions dynamically to those files. I can't have all the code in one file, because each job application requires a unique form with different questions and different job description.

like image 881
Joel Gross Avatar asked Sep 29 '22 05:09

Joel Gross


1 Answers

I'm assuming that you mean a script file in a totally different project file, or even a different Google Drive? A script in a different script file, that's in the same project can be triggered just by calling the function name. Apps Script does not need to include one script file in another script file. All GS script files are available to every other GS script file.

It's possible to use an Apps Script project as a "Stand Alone" Content Service App and get a return value. The Apps Script project must be called with an HTTPS GET or POST request. For example, you could do that with an AJAX call from the client side, or from server side code.

Apps Script has a urlFetchApp.fetch method which will issue either a GET, PUT, POST or DELETE request.

Google Documentation - urlFetchApp

The original HTTPS call can only trigger either the doGet() or doPost() functions. But those functions can call a specific function and then send the return.

Stackoverflow answer - return from a specific function

It's also possible to create a "library", which is reusable in multiple scripts.

Google Documentation - Libraries

You can cause script to run automatically when a Sheet, or Doc opens. So, if the new Apps Script was in a sheet, and the sheet had an onOpen() function the function could get triggered that way. But then you'd need a way to open the sheet or doc.

The doGet() function which runs automatically when a Stand Alone Apps script URL is invoked in the browser can take URL search string parameters. So you can put a search string parameter in the URL that is calling the Apps Script, and then the Apps Script can do something according to the search string value. Like if you want to open something other than the home page when the app first runs depending on some condition that the user choose.

like image 57
Alan Wells Avatar answered Oct 03 '22 22:10

Alan Wells