Duplicate Namespace Including Google Apps Script Library - google-apps-script

I've forked this OAuth2 library for Google Apps Script, uploaded it to my Drive as a separate Script, and swapped the SCRIPT ID of the upstream repo with my fork to test changes I've made to the library. However, when I run functions that call library functions, such as OAuth2.createService, I get this TypeError:
Cannot find function createService in object [object Object]
The OAuth identifier is another level down, inside another OAuth object. So the script works with my library when run OAuth2.OAuth2.createService. My fork doesn't have any changes that would do this.
It's not the biggest deal, but I want make a pull request with this sample code. So my question isn't so much about why isn't this code working, it's why is GAS include essentially the same library in these two different ways?
Here's how I've included the libraries in my script.

I'm sure that you are using the built code instead the library directly. You need to modify the code from src folder. Skip dist folder always.
Issue #70

Related

How to handle callback from HTML file in a Google script library [duplicate]

I'm just starting to use libraries in Apps Script. I can get libraries to work when the library code is referenced from a .GS file but the I cannot get libraries to work when accessing them via .HTML files (I get a message xxx is undefined)
Before I spend anymore time on this can anyone confirm that libraries are only for .GS files
thanks
You cannot call library functions directly, but if you create a function that calls library functions on the Google Apps Script side,
google.script.run.withSuccessHandler (onSuccess) .gasfunction ()
Can be called with
There are various uses.
Reference links:Library functions cannot be called directly
Html libraries are different from server script libraries. There is no point in using server side library in html. Even if it is defined, it'll be useless. You could however call the server library from html(client side) through google.script.run

Using an Apps Script library in a webapp html file?

I'm just starting to use libraries in Apps Script. I can get libraries to work when the library code is referenced from a .GS file but the I cannot get libraries to work when accessing them via .HTML files (I get a message xxx is undefined)
Before I spend anymore time on this can anyone confirm that libraries are only for .GS files
thanks
You cannot call library functions directly, but if you create a function that calls library functions on the Google Apps Script side,
google.script.run.withSuccessHandler (onSuccess) .gasfunction ()
Can be called with
There are various uses.
Reference links:Library functions cannot be called directly
Html libraries are different from server script libraries. There is no point in using server side library in html. Even if it is defined, it'll be useless. You could however call the server library from html(client side) through google.script.run

Using Google Apps Script Libraries

I have read all Google documentation on managing and creating libraries, yet I still do not know if they are an appropriate option for the problem I am trying to solve.
I know how to save a version of a standalone script. I know how to add the library to a spreadsheet via the script editor. But I don't understand, very simply, how to trigger the library script within the new spreadsheet.
I have a spreadsheet that serves as an often-copied template within my organization. The template contains a script that (onOpen) accesses data on a separate spreadsheet (a master database) and sets those values on a tab called "admin." The desired result is to have a copy of the master database living within the template sheet (and every subsequent copy of the template sheet). At this point, there are thousands of copies of the template sheet, each running that same script.
Whenever I have to change the script, I have to change it within thousands of sheets. Can I use a library instead? I'd like to be able to create a new version of the script in the library and have all sheets connected to that library experience the change. I understand that the library needs to be in development mode (within each sheet) to do this. I also understand that in order to make this switch, I will probably still have to go into each sheet to add the library. I'm just hoping it will be the last time I have to do such a tedious task.
Any advice or links to solid info is appreciated.
besides making an add-on (already covered in another answer) I will answer your libraries question. They will work for you. What you are missing is the "connect" part.
For this you want to trigger the library code from say, onOpen. The onOpen in the library is not enough and not detected by apps script. Instead each of your spreadsheet's script needs an onOpen(e) which just calls yourlibrary.onOpen(e).
since those "hook" calls rarely change, specially once you stabilize your library api, and using it in "development" mode will let you modify just the library.
whenever one of those hooks needs to change (say a callback from an html GUI needs a new parameter) you need to update all the spreadsheets. to avoid this, make all your callbacks receive a single json object instead of multiple parameters.
Sorry if I am repeating other answers, but I would like to sum up and add something:
You can access your library functions as follows:
From the app using the library you go to the Resources/Libraries. You can see the library name under "Identifier". On the same line where you can select Development mode.
Library name found in resources
Now in your library you have for example a function
function onOpen(e)
{
Browser.msgBox("HELLO!");
}
In the spreadsheet app you wish to access it you use the library name found in the resources, for example "testlibrary"
function onOpen(e)
{
testlibrary.onOpen(e);
}
Now if you have development mode on, the modifications to functions in the library update automatically to your application (spreadsheet) as long as the user using the application has edit access in your library script.
If anyone using your spreadsheet has a restricted access to your library script (meaning only view access) or development selection is off in the application, you have to go to the application's script, Resources/Libraries and select the most recent version of your library to be used in the app everytime you update the library and save a new version of it.
Still, especially if you are using mostly only the onOpen function , I would recommend using the library rather than copy-pasting the function to the script of each spreadsheet, as it is easier to track which scripts are up to date and it is easier to avoid errors and differences between the scripts.
Even in the more restricted case, if you update function in library - as long as you are already calling it in the app - all you have to do is select the new version of the library used.
I hope I had anything to give in this conversation and my language was appropriate, this was my first answer..
A good question Melly. I've been through a bunch of the documentation and some tutorials on this subject but haven't tried adding any libraries yet. My understanding is that once you are connected to a library all you have to do is call the applicable functions. That once a library is connected the functions in the library become an extension of all the other Apps Script classes available in the script editor.

Google Apps Script how to call external .gs

Starting this question from scratch with a better explanation:
I have two scripts - one is the primary file and the second is basically a library I want "copied" to the primary. Here is my current code:
Code.gs
function doGet(){return HtmlService.createHtmlOutputFromFile(0)}
function nP(page){var pageOutput=HtmlService.createHtmlOutputFromFile(page).getContent();return pageOutput}
0.html
<body>First Page<button id="p2">Second Page</button></body>
<script>document.getElementById("p2").onclick=function(){google.script.run.withSuccessHandler(cP).nP(1)}</script>
1.html
<body>First Page<button id="p1">First Page</button></body>
<script>document.getElementById("p1").onclick=function(){google.script.run.withSuccessHandler(cP).nP(0)}</script>
My goal is to have an external .gs file with the nP function that I will be able to use in other web apps (this being a start, having this ability would be extremely useful with other functions also). I'm looking for an option similar to HTML's where it acts like it's written in the file if possible for simplicity. Or I can use the library option if it does in fact work, I've just ran into issues where it says the function doesn't exist, invalid variable or invalid return.
There is a way in which you can call external .gs files and it will work just like that external script is written in your script's another .gs file.
For that, you will have to use UrlFetchApp.fetch(String url) method.
What to pass in String url? -> External script's deployed as web app URL. Yes, you will have to deploy other .gs file as web app and when you update it, it shows an URL, copy it and use it in your fetch method.
To pass values to external .gs file, you can use ?xyz=abc and then in that external script, use doGet(e) and then e.parameter.xyz;
Just make sure the files which are getting accessed by the script are available to each script properly.
Refer this: UrlFetchApp method
The way to call a code in external .gs files is by adding the project that contains them as a library.
As alternative you could use
Google Apps Script Execution API
Publish your other Google Apps Script as a web app

How to use one code for multiple spreadsheets that can be updated, attempt to use Library

I have 200+ spreadsheet that our customers fill. I developed a script to manage the spreadsheets which is basically identical in function.
The problem is when I modify the script I need to change it in all these sheets. I tried using libraries but with no success and I hoping to hear if someone knows an answer.
The library version I include (with development on) in each spreadsheet does not show the change ensued. It will only show the results at the time of inclusion, which means that in order to make this work I have to go to each of the spreadsheets, remove the library and re-install the most recent version. The updated library only works from my owner's account. The library is of course shared by anyone who has a link.
Running a library's updated function in any of the spreadsheets produce an error:
"TypeError: Cannot find function FunctionName in object [object Object]. (line 2, file "test")"
Is there a good way to have all spreadsheets same one code that I can change whenever I wish?
You should turn on "development mode". Resources -> libraries included for each spreasheet that include the library
I have not had much success using library myself.
One solution I would suggest would be to use a standalone script. If the spreadsheets have identical features, you can run the script on all of them using a for loop.
You can get an array of files inside a folder using using getFiles()
// Logs the number of files in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var files = folder.getFiles();
Logger.log(files.length);