Can I add a .js library? how? - google-apps-script

I have some code from gitHub I'd like to run in GAS, but it uses a library (chance.js) I don't know how to install.
I have looked at the documentation here, and tried to follow the steps but I don't have a project key (because nobody's used it in GAS?).
I see that I might be able to call it from my script (?), but I don't even know how to start with that - I am a novice at this.
Is there a simple way to add this library? Can you point me to any resources?

Download the development version of chance.js. Open it and copy the whole text. Then go to your Apps Script editor, create a new script (File/New/Script File) and paste the text into there. Then at the bottom of the chance.js code (but before the closing parenthesis) you need to add chance = new Chance();
btw. In Apps Script every single time you start a function manually or through a trigger, all the js code in all js files belonging to the project is being executed and only afterwards is the selected function run. Of course that doesn't mean that the code inside of all the functions is being run.
edit:
Here is a better approach.
Change the start of chance.js from (function () { to Chance = (function () {. And then change the end from })(); to
return Chance;
})();
chance = new Chance();

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

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).

Call functions from another "class" / file

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.

QTP, Is there anyway to make changes to all function libraries at once ?

is there anyway in QTP to make changes take place on all function libraries at once without having to go to each function library and make the update ?
i have an issue with some functions, i have built new ones and i want to use them in my script.
your help is highly appreciated.
thanks
Manaysah
AFAIK, in QTP there is no such way to update code present in multiple tab whatever it may be function library , action or reusable in multiple tabs. You have to proceed manually. You can use some advance Editor like Notepad++. But again you can make changes only in code & not in OR. It is easy to such task for datatable as it can be directly open as excel file.
If you want to update only code, then you can open required function libraries in notepad++.Then Go to Search - Replace (Ctrl + H)& then Click on Replace all in all open documents.
Hope this will help you.