Need help using a google script across all google spreadsheets without copying script to each sheet. I've converted the script project to a cloud-managed platform and enabled all the APIs that I think are required.
I've deployed the script as add on and I see it on the add ons menu in sheets but I can't call my functions, etc.
Within one project you can have several different script files (You can create them by going to File>New>Script file). I suggest you distribute your functions and your logic across them the way you most prefer. Afterwards, any of those functions can be accessed by any of the other files declared in the project, so you should have no issue on calling them from i.e. your Add-on.
Additionally, you can use libraries. To do so:
Create a new Project (which will serve as a library). It may have multiple gs files, with multiple functions.
From the guest script, the one that will use the library, go to Resources>Libraries. There, insert your library project's id, and the identifier that will be used (variable name) to reference it. For the example below, I have set it to be MyMathLibrary (see example usage in Invoicing.gs).
Example project:
Project link: https://script.google.com/d/1kNeHHS7M19ILwcbbootMfZFNtg0NdE27WtkElXFBgOVR6haUUKPYqOBL/edit?usp=sharing
Library link:
https://script.google.com/d/12x33E1q3Uk2dgx5SfVTllVel8PeHcudQ-Czwhnu7aJPU5-tFpICuEcMM/edit?usp=sharing
Related
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.
I have a Google Apps Script project that is used as a library by other projects.
I have annotated, on a separate document, where this script is used, but I cannot find a way to double-check this list.
Is it possible to perform a search, using the project ID or other elements, returning all the other projects where my script is referenced as a library?
I tried using search in Drive, but I was unsuccessful.
its not possible to search for which projects use your library. At most your library could create a log of when it's called (but not who calls it), and those using the previous version wont pick that change so you cant even log those calls.
I will be maintaining Google spreadsheets for 100 different schools. Each school's spreadsheet will have similar functions (e.g., jumping to a certain cell based on the date or creating a note in a certain cell based on inputted text) and multiple people will have access to edit it (e.g., principal, teachers, coaches, etc). I imagine that creating a library is easiest for this purpose since the functions are the same for each spreadsheet! For example, the script file for each document uses the onOpen() event to call .addMenu(). Then .addMenu calls a wrapper function and this function calls the "jumpToToday" method in my library. (I saw the posts about how the library method cannot be called directly from the menu, so that is why I did it this way). I have two questions:
When I (the owner) share the spreadsheet with an editor, the menu shows up, but when the editor selects one of the menu options, the script returns the error:
You do not have access to library ExternalSupport, used by your script, or it has been deleted.
After I saw this, I shared the library file (Can View) with the spreadsheet editor and then it worked. Do I have to share the library (view only) with ALL editors of the spreadsheet so that they can run the functions? If so, that's fine (I was just trying to avoid it because most principals/teachers/coaches are not techie, so I didn't want to confuse them by sharing a file of code...I just want them to use the spreadsheet).
Because the editors of the spreadsheet will only be able to view the library, it seems like they only have access to the Version that is selected (even if dev mode is on).
When I update my library to another version, do I have to go into the Script Editor of all 100 spreadsheets and then go to Resources > Manage Libraries and update the version? Or is there a way to make all files use the latest version of the library?
1) Yes. According to the documentation
you must grant at least a read-level access to your project for all potential users.
I agree that this may be confusing for other users but at the moment it is necessary.
2) There is no way to make a file use the latest version of a library. Though, there is a way to make a file use the development version of a library. For this to work you need to grant editor-level access to your library to the user. However, bear in mind that any changes that you make to the library will be reflected immediately at the user's end. You can find more information in the documentation.
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?
Please forgive me if I sound naive, I did read some tutorials on Google Script site.
I recently became aware of extending Google Spreadsheets functionality with custom functions using Google Script.
I understand the Script is tied to a particular Spreadsheet that one opens in Google Docs and using Tools menu to insert custom functions code. The Spreadsheet works like a container for custom functions code
The issue that I am having trouble understanding is how such functions are published and how do the end users discover availability of custom functions so that they can insert those custom functions in the spreadsheets they create
And is there a way to put together a collection of custom functions to work like a Functions Library or do we have to create individual functions and publish each one
As you've mentioned, a custom function's availability is restricted to the spreadsheet under which the custom function was written until recently.
You can now create Libraries of the functions and make them available for other spreadsheets to use by the script ID - More details in the documentation https://developers.google.com/apps-script/guide_libraries
If you publish your script to the Script Gallery it can be discovered and installed by other users. They will have a copy of the script's source installed into their spreadsheet.