Call functions from another "class" / file - google-apps-script

Ok, I feel embarrassed that I wasn't able to figure this out on my own, but after a few wasted hours, I figured it would be easier to simply ask over here:
I have a bunch of .gs-files in my Google Apps Script project. Now, I want to call another file's function from a method (something like AnotherClass.awesomeFunction(), which throws a ReferenceError though). Is this possible in Google Apps Script? If so, how?

Files aren't classes. You can call any functions in any file from any other file. Think of your files as if they were just added together before running. If you want class-like scoping you can use the Libraries feature.

The Above replies are correct above file being appended, make sure the order of the files in the file explorer on the app script project page is correct
The Function definition should be in the first file and the function call in the latter.
You change the option of the each file by clicking the 3 dots next to file name and selecting Move file up or Move file down

The following syntax allows you to call any function from within your Google Apps Script project, regardless of whether the function is defined in the same file that is calling it:
myFunction();
The following code is unnecessary and will throw errors:
google.script.run.myFunction();

It can do.
and Corey is right, files is not class.

I'd just like to add that order of files is not important as experienced by me so far. I'm working on a project where all calls are at the start to get a clear tree and all definitions of functions are at the end. Sometimes they're even mixed without any order within files too. So, I guess, it can call function from anywhere regardless of order within file or within project files. It's working in my case though.

Related

How to include a file into your project

I'm creating a Google Apps Script that's kind of a quiz marker / email merger in one. I've got everything working but my code is pretty long because I find myself having to use procedural coding. This is mostly because I don't know how to use the editor to include a class / library.
I've viewed the Crash Course Video by Kalyan on using the Script Editor, but it didn't answer my question. I've also scoured the internet with no luck.
If you look in the top left of this screenshot, you'll see I have Code.gs and test.gs:
I have all my code in the Code.gs file, but I want to include test.gs in Code.gs. Once I figure out how to do that, I'd like to create a couple of simple classes to organize my functions a bit better (and possibly re-use my classes later).
Thanks in advance!
To clarify Zig Mandel's answer and add to it:
All .gs files that are located in the same Script Project have global access to each other. That is to say that a function that you define in test.gs is accessible by Code.gs if they are in the same Script Project
You can create your own library of code. To do this you would simply create another Script Project, then go to Resources -> Manage libraries..., and add the project key of your original project (that contains the Code.gs and test.gs files). The project key is located in File -> Project properties.
The second bullet point is a good way to organize your code. If you have the same code that you are reusing throughout many Script Projects, then you want to put those functions into one Script Project and create a library out of them, as I explained above.
You already included the code. Should work as is. You can also look at the apps script docummentation and learn to use libraries for code reuse:
https://developers.google.com/apps-script/guide_libraries.
This makes development easier but for best performance you should include them all in your project before publishing like you did this time.
Part of the problem may be the order of execution. The .gs files are executed in order of creation. Copying Code.gs, deleting the original, then running functions from the new copy may fix your problem.
<?!= include('JavaScript.html'); ?>
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}

How to run a function in javaScript when all files have been Uploaded

I am using a primefaces FileUpload's the sending of multiple files. I would like to know how to perform a function onComplete when all files have been transferred.
I am creating a p: dialog that runs at the beginning, and if I use the onComplete, after the first shipment, it already performs the function. I would run the function after all the items you were completed.
Anyone know?
Edit:
It could also, if I could spend the entire file that was inserted in the list, could control the event from the Bean. Anyone know how I can pass the full list of files included in the upload?
I guess it's not possible. Take a look at this, there's an patch to do what you need: https://code.google.com/p/primefaces/issues/detail?id=3836

Custom module undefined functions

I am developing a custom module for my employer. The my_module.module fill got too big and unmanageable so I decided to separate related code into separate .inc files.
I then included those files in the following code:
function my_module_init(){
module_load_include('inc', 'my_module', 'questionnaire_nodetypes');
module_load_include('inc', 'my_module', 'questionnaire_config');
module_load_include('inc', 'my_module', 'email_friends');
}
My code continued to work for a while but I suddenly got a undefined function error for a function that was definitely present in questionnaire_config.inc. Other similar errors soon followed for other confirmed and existing function in the other included .inc files.
It was suggested to me to run the update.php script but this did not fix the problem. In fact, I got more undefined function errors.
There's no need to put those lines in a hook, just dump them straight into the top of the module file (I've been doing it for years and it's never caused an issue).
hook_init() doesn't get called for cached pages, so it's conceivable your files aren't being included when another (non-cached) part of the system is relying on the existence of those functions.

Organizing Spreadsheet Code in several *.gs files - even possible?

I am trying to organize my code for a Spreadsheet in several script files. Within the script editor I can create as many *.gs files as I want, but I can't figure out how to access code that would be defined in another script.
Simple Example of what I'd like do achieve:
Code.gs:
function onEdit(){
myFunctionFromLibrary_gs();
}
Library.gs:
function myFunctionFromLibrary_gs(){
Browser.msgBox("hi there");
}
The onEdit() is obviously called by a Trigger.
Without modification this will result in a Runtime-Error, stating that
myFunctionFromLibrary_gs TypeError: is not a function, it is undefined.
So how can I make this work, or is this currently not supported?
Thx in advance for your help.
Yes, it's possible.
You are not limited to a single server Code.gs file. You can spread server code across multiple files for ease of development. All of the server files are loaded into the same global namespace, so use JavaScript classes when you want to provide safe encapsulation.
Reference: Google Documentation - features and limitations
I don't know what the _gs suffix means for Google, but without it (see code bellow), the code works.
file1.gs:
function onEdit(){
myFunctionFromLibrary();
}
file2.gs
function myFunctionFromLibrary(){
Browser.msgBox("hi there");
}
I know this is an old question but I found it looking for a similar task and happened to find the answer during my same search.
From the docs at https://developers.google.com/apps-script/guide_libraries#writingLibrary:
If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, myPrivateMethod_().
While your function does not END in an underscore, it may have special meaning in other places than just this, or the _gs suffix may also have special meaning (particularly given the same filename suffix).

Why can't Matlab see my function?

My function is definitely working; it's tested and was at one point being recognized.
Here's the function prototype:
function [X Y] = calculateEllipse(x, y, a, b, angle)
%# Code here
end
Here's the call I'm making from the Matlab terminal:
calculateEllipse (612, 391, 107, 60, 331)
Here's the error popping out at me:
??? Undefined function or method 'calculateEllipse' for input arguments of
type 'double'.
Now, I am 100% positive I am in the same directory as the function. I even used
addpath('C:\path-to-function')
to make sure. It's just not working, and I'm baffled.
Any help is appreciated.
To summarise other posts, here is a workflow for determining the cause of the problem.
You mistyped the name of the function. Check the function definition and make sure it really it called calculateEllipse.
You saved the function to a file named something other than the function name. Check the filename of the function and make sure it matches the function name.
The folder containing the function name isn't on the MATLAB path. There are several ways to check this. Type path to see the current path, or which calculateEllipse to find the location that MATLAB is using for that file. (If there is a problem, that last command will display 'calculateEllipse' not found.. Note that addpath does not permanently update the path, so when you close down MATLAB, the path will be reset. Use savepath for this.
The folder containing the function is a subdirectory of matlabroot. These folders are reserved for fully fledged toolboxes; bad things happen when you store your code here. See Bob's answer for more information.
Other useful things to check are:
Can you call other functions that are stored in the same folder?
If you save the function in a different folder, will it run then?
Adding to what Jeff said; another possibility is that you placed the function somewhere inside of your MATLAB installation. By default MATLAB does't re-search its own file structure for new files; it assumes that its internal file structure remains unchanging. Make sure that you're saving the file (which, as Jeff pointed out, must be named calculateEllipse.m) somewhere outside of your MATLAB installation.
See https://www.mathworks.com/help/matlab/matlab_env/toolbox-path-caching-in-the-matlab-program.html, or go to the MathWorks web site and search for
path cache
for more information.
The key to this problem is this: %Has no license available. This implies that a function in the directory of the function you are trying to use has the same name as a function in a toolbox you do not own. MATLAB by default disables the whole directory and not just the function of the same name in a toolbox you do not own. Here is an example:
files in directory:
myfunction.m
scoobydoo.m
blackman.m
If I do not own the "Signal processing toolbox," then blackman.m will disable the whole directory.
I can think of a couple of reasons this could happen.
First, as Jeff said, you could have named the file 'calcEllipse.m' instead of 'calculateEllipse.m'. In which case you need to rename the function to be the same as the m file you saved.
Second, you have not added the correct path. There is no reason for this to give an error to my knowledge otherwise. Double check that you have added the path to the m file that is being saved. An easy way to check is if you type in 'calculateEll' and then press tab, does the autocomplete work? If not you are out of the path.
Hope it is one of those thing you can quickly fix!