I have few sheets that have functions bound to the spreadsheet. Each spreadsheet has its own functions and uses SpreadsheetApp.getUi to run html service. I would like to initiate function calls in all the sheets from a master spreadsheet project? is it possible? Something like getting a handle to the spreadsheet project and run a script in that project?
You have two options:
Publish your scripts as libraries and subscribe to each in each other of your script projects.
Publish your scripts as web apps with specific functions as individual pseudo webhooks. Sort of like a distributed API.
There are pros and cons of each. Neither is about maintainability really.
The library option will afford code completion whereas the web app option will enable (if you wish) for you to run code asynchronously.
Both have different speed penalties. Library enabled scripts are slower as described in the documentation. Web apps will be slower because of urlfetch latency.
Library functions will use the runtime allowed for them in the host script, whereas web apps will extend runtime and some quotas.
Documentation:
Publish your scripts as a library
Running apps script as an endpoint
using library seems to me is the fastest and simplest way to protect your code.
for this you need :
1 spreadsheet or any google doc containing Code - SCRIPT A
1 stand-alone script SCRIPT B that you publish as a library.
in the editor of SCRIPT A -
you add the library Key
in the SCRIPT A code you can call the function of SCRIPT B
function callFunctionOfScriptB(){
LibraryIdentifier.functionNameinScriptB()
}
you find LibraryIdentifier when you click on Resources + libraries in the column Identifier of the popup
functionNameinScriptB = the name of the function you want to call in Script B
Related
I have a Google sheet with some code in a bound Apps Script. The code contains some functions that are used in a nightly trigger, and some custom functions used in sheet formulas.
I'm trying to have a sane development process for this, and to decouple saving code from releasing it - so I have a "production" deployment, and the trigger runs code from that deployment - when the code is ready, I update the production deployment with the new version (I use clasp to manage this process). This works fine.
However, I don't know which deployment of my Apps Script code does Google Sheets use when running custom functions? Does it always use code from the #HEAD deployment? Is there a way to control this?
On bounded scripts custom functions use the head deployment, on add-ons use the version specified in the Google Workspace Marketplace Configuration page..
I'm working with apps script and the plivo api (https://www.plivo.com/docs/sms/api/message#list-all-messages). I'd like to be able to use the plivo node sdk (https://www.plivo.com/docs/sdk/server/node-sdk/) inside apps script. Is there a way to install this or use this from github or some other source directly?
Answer
In a summary you can't install or use plivo by using Apps Script.
You can use your own libraries or a shared library. Third party (external and not shared) libraries in Google Apps Script are not allowed directly.
As per the Google Apps Script documentation says Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with G Suite. Apps Script goal is mainly interact with other Google Services/APIs.
Why using third party libraries is not allowed/recommended?
It's possible to use the eval and UrlFetchApp.fetch() function in order to get the content of a specific JS and execute it like:
eval(UrlFetchApp.fetch('http://URL/javascript.js').getContentText());
However, it's adding more computing time when it comes to run an Apps Script and depending on your kind of user there's a limit to successfully execute a script.
External services like plivo or another VOIP services are not allowed due to the reasons I mentioned above. As a workaround I'd suggest to take a look into GCP products like App Engine or Compute Engine and make use of your third party library from there.
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 call an external Google Web App script from within my script in a Google spreadsheet container? I have a number of spreadsheets that are created dynamically and would like them all to call a single Web App for uniformity and ease of maintenance.
Have a look at this blog post: Trigger updates between master – slave spreadsheets
Basically what you want to accomplish in the same, in the specific scenario the master spreadsheet is actually a web app (web service) for all the slave spreadsheet documents.
You are describing a textbook use of Google Apps Script Libraries! Start by reading Introducing Versions and Libraries in Apps Script.
Your master script, plus any utilities you want to access, needs to be set up as a library, for example let's say it's called MyLib. Your dynamically created (client) spreadsheets then access functions that are in MyLib by treating it as a javascript object. For example, a function calcValues() would be referenced like this:
...
var result = MyLib.calcValues(dataRange);
...
You didn't ask this, but just in case you're thinking of using custom functions (as described in Custom Functions in Spreadsheets) that are implemented in your library / master script, you will need to provide wrapper functions in your client spreadsheets. Here's that same example as a wrapper:
function calcValues (dataRange) {
return MyLib.calcValues(dataRange);
}
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.