Google Apps Script how to call external .gs - google-apps-script

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

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

Duplicate Namespace Including Google Apps Script Library

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

GAS - How to copy a Docs file with a bound Script and execute that script

I need to programmatically create a file based on a template Google Docs, and then process the document with a bound Script.
My initial approach was to create a bound script to the template file; then, using the google client library (for php, ruby, or w/e), copying the template into the actual document, and finally call the Script function on the newly copied document.
The problem is it seems you have to "publish" your bound App Script, and even after copying a document with a published bound script, the resulting file's script is not published.
Is there any way to copy the document and publish it's bound script directly from my PHP/Ruby/etc app?
PS: The reason I'm using bound script rather than just a script, it's because I need to store data using the PropertiesService.getDocumentProperties() in order to replace some variables with a "preview" function
I think there's a misconception about boundscripts. You call them boundscripts because
" it was created from that document " Tools -> Script Editor unlike standalone scripts which are created from script.google.com directly.
You don't have to publish boundscripts in order to use them. Those are called add-ons.
Now with regard to copying documents in Apps Script, try checking out copy() from DocumentApp. There's also a code snippet available in this SO thread with that demonstrates copyong which might give you insight.
Hope that helps.

Is it possible to read the contents of a Google Apps Script from another Script

I'm looking for a way to read the contents of the script.gs files within a standalone Google Apps Script. I can't find a suitable getAs() Mime format in the Docslist.File service that doesn't fail. The context of this is that I want to use the html service/content service to be able to embed public GAS source samples in Google Sites and elsewhere. (in much the same way as embedding Gist Samples). Any ideas?
It is, in fact, possible. This gets Code.gs:
function myFunction() {
throw ScriptApp.getResource("Code").getDataAsString();
}
There is no way to programmatically retrieve the contents of a ".gs" file. You can only ask for files with ".html" extension and potentially you can return it in a project.
This request has come up in the context of allow local development or Git integration to provide an API way to get/set script source in a file. Please put your comments in this issue tracker request or log a new one if it makes sense .
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1108