Is it possible to list all scripts associated with a GDrive File though the Apps Script File Class? - google-apps-script

I was wondering if there's a function like file.GetName(), however it would be something like file.GetScripts() to be able to find if a file has container-bound scripts.
see: https://developers.google.com/apps-script/reference/drive/file#getDescription()

I attempted something similar this past summer (summer 2014) and determined it was currently not possible, either via Apps Script nor via the Drive SDK.
In our case we were able to re-structure our project so the scripts we needed to interact with programatically were stand alone files on Drive, which we include in the relevant documents as Libraries.

Related

Is it possible to upload a .gs file to an apps script project of a copied google doc?

I copied a spreadsheet to be used by multiple people with script attached. My question is how do I modify script in the project without having to copy and paste each change to .gs files in the project?
This is the base document from which the other versions were copied:
Create a central script and then publish it as a library. Have your template linked to the library. Make sure that the template is using the "dev" version of the library so that any future changes will be reflected for everyone.
There ARE ways to manipulate the GS files using clasp but it is much less straightforward than using libraries and overkill for this use case.
https://developers.google.com/apps-script/guides/libraries
Yes, it's possible to "upload" a .gs file to an existing Google Apps Script project. To do this you might use CLASP, GAS GitHub Assistant (a Chrome extension) or directly the Google Apps Script API.
Please bear in mind that you also could alleviate these pains (having to update the bounded to spreadsheet GAS project copies) by taking a different approach. You could use libraries or to create a Workspace Editor add-on.
Libraries help by keeping a big part of your code in a single place but you might still have to update the copies i.e. if you are using simple triggers like onOpen and onEdit or if you change library function name, add a new function, etc.
Add-ons help by keeping all the code in a single place bu you have to publish the add-on to the Google Workspace Marketplace. If you have a Google Workspace account the add-on can be published for internal use.

How can I Find All Uses of a Google Script Library?

I have a Google Script library that is used by at least 100 other scripts (some that are bound to spreadsheets/documents, some that are not). How can I find all of these client scripts that reference my library script?
More specifically, I need to be able to add a new feature into the library that requires new permissions that I (the user) must grant. The client scripts won't run if I just add this feature to the library without granting the permissions to each of the client scripts. So ultimately, I need to give this new permission to each of the clients. And if I knew what scripts were actually using this library, I could do that manually for each one. But I need to URL's or ID's or something for each of those scripts.
Answer:
Unfortunately this is not possible to do.
More Information
It is possible to get a list of standalone Scripts from your Drive, though scripts bound to a file can not be searched for using regular searching methods.
It is possible, using the help of this Google Account page to get a list of all the Apps that have access to your account, though only files you have authorised will appear here, and apps which are not just those created by you in Apps Script will appear there (for example, other add-ons or even Android Apps bound to your account appear here).
A Partial Workaround:
Using Google Apps Script, you can list all Apps Script Projects that you own with help of the MimeType enumeration GOOGLE_APPS_SCRIPT
var scripts = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);
var arr =[ ];
while (scripts.hasNext()) {
var script = scripts.next();
arr.push(script)
}
Logger.log(arr);
Or even just searching for type:script in Drive, however this only returns a list of scripts that are not bound to a file.
You can then use regular Google Drive search terms to find which of these files contain, for example, a unique method name that the library uses. I am aware this isn't a perfect solution and you would still have to look for projects bound to a file using the above webpage.
Feature Request:
It appears that back in 2014 a feature request for this was made on Google's Issue Tracker, though I would suggest creating another feature request for this here as it was marked as a duplicate of another issue. You can make a feature request under the correct Apps Script component here.
References:
Google Apps Script - Enum MimeType
Google Drive Search Query Terms
Apps with access to your account
Google's Issue Tracker
Feature Request: Listing and searching for container bound scripts
Create an Apps Script Feature Request

How to separate script editor from document (google apps script)

I'm writing some script for my Documents, Spreadsheets and Gmail. I would like to know if there is a way to save all my script to separate files in a specific google drive folder. I don't like having to go to the backend of my Documents and Spreadsheets in order to edit my script.
Would I have to call the document from my script using "getByID" or is there another way?
Yes, when you create a new document in Drive you can select Script. If you don't see the script option, try going here.
You will, however, need to call your document using getByID. There my be other complexities if you are using onEdit type triggers.
A lot more information can be found under the Types of Scripts heading at Google Apps Scripts Documentation.
You could go advanced and pull your script in behind the Document as a Library and then using the script behind the Doc to make the calls to the library, but that's a bigger discussion.

How to use Google Apps Script on any/all Spreadsheets?

I am trying to implement a spreadsheet app using Google Apps Script. But I have several issue that does not mach my requirements. I have looked in to these issues and was unsuccessful to find answers or better alternative ways to implement this functionality.
Requirements:
Write manageable testable code
Not to download the script to each spreadsheet but to somehow when you install once you can use it on any spreadsheet in the drive.
1,2 are the problems I faced.
The Google Apps Script IDE seems to have difficulty in handling a large code base (I mean not to keep the code its about managing and handle the growth of the code)
This is the major problem (Not matching the requirement)
Let's say I create a script for a spreadsheet and then I deploy it and then I can then install the script on any other script I like and then execute it. But this has to be done for each and every spreadsheet. The installed script is not in the script manager to be used in all the spreadsheets without installing the script form the gallery every time.
And also When I create a project of Spreadsheets directly on the Google Drive. Since this is not allocated to a particular spreadsheet it does not allow me to select it from the dive (From script manager) and load this script to a spreadsheet I wish, and run/debug it.
I am not hoping to get a better solution to above issue 1.
But issue No. 2 is is the burning one,
Is this can be addressed to match the requirement?
If the requirement can not be achieved form Google Apps Script, I have to go for an alternative.
Alternatives I thought so far:
I thought about desktop application that can give the spreadsheet URL and then give the authentication information and by using the spreadsheet API do the necessary manipulations form the desktop application. This is the dummy est approach which is not so user friendly.
Go for a Google app, where you can log in using the G mail authentication and then after giving the spreadsheet URL using the Spreadsheet API do the necessary manipulations. But I am not sure about the authentication process for a Google app and the possibility of access permission to the drive.
Final is the Crome app, Where Crome is provided with the necessary authentication to access a given spreadsheet URL and do the necessary manipulations to the spreadsheet.
Which of those options is preferred, or is there any better solution that matches the requirement I stated above?
The definition of "large code base" is subjective, but even a project with several hundred lines of code can benefit from organization. In the IDE you have two primary tools available to help organize your code; Files and Libraries.
This answer is mainly a collection of links to other answers, within a number of topic areas that should address the concerns you've expressed.
Code Organization & Libraries
A script project can have multiple files, of type "gs" (script) or "html". The namespace extends across all script files in the project. Separating methods into files is a simple way to organize your code within a project (for instance, place all tests in one file separate from production code), although it does very little for reusability across projects.
Libraries are projects that are published to enable other projects to incorporate their capabilities without requiring them to copy the actual code. Have a look at the Notable Script Libraries for a start. Libraries take their name space from the library name - for instance, I have a library of utilities related to input & output of CSV files, which I (cleverly) named CSVUtils. The methods in that library are accessed as CSVUtils.example(), and are supported by the auto-complete feature. Libraries support a limited subset of jsdoc, and when published provide a link to the generated documentation.
Organizing Spreadsheet Code in several *.gs files - even possible?
Is it possible to have one script for multiple spreadsheets?
How, or where, do I define a function for use in more than one spreadsheet?
google-apps-script have multiple scripts in one spreadsheet
Creating a namespace-like organization in a google apps script library
Publishing a Google Apps Script Library
Design patterns for libraries in GAS?
ScriptDb in Library accessed by WebApp
Project and multiple doGet() script files published as web apps -- Need clarification
... every SO question about Apps Script Libraries.
Using external JavaScript libraries:
Start by reading through Understanding Caja Sanitization, especially the section regarding External JavaScript Libraries.
Google Apps Script: How can use js library in HtmlService
Authorization & Ownership
Authorization - Google apps script grant user permissions access services on spreadsheet in ther first run without open script editor
https://stackoverflow.com/questions/15096379/google-app-scripts-how-to-let-shared-users-run-scripts-as-themselves
When does Google require re-authentication?
OAuth in Google Apps Scripts Libraries
Code Transparency
Source-code of Libraries used by WebApps visible to everybody?
How to hide library source code in Google way?

How can I upload a dynamically created Google Apps Script?

I want to create a script dynamically and then upload it as a Google Apps Script.
When I try this, either
If I set the mime type correctly the link looks good, but doesn't work (file not found).
If I create a text file, that is just what it is - a text file, not a script file.
Do I need to wrap my code in an extra layer so that the editor can work with it?
Thanks
Michael
There's a way I've seen people implement something like this, but do so at your own peril: UrlFetchApp and eval(). You end up having to host the script somewhere else, pulling it back, and hitting eval() on the script somewhere. You may be able to do this via Google Drive sync and the DocList API.
An Apps Script API within Apps Script is something we've heard feedback for and are talking about, FWIW, but don't let this block you from getting what you need done in the meantime.