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..
Related
I am trying to get a variable to work in all instances of all scripts across my user account to prevent unwanted scripts in different documents running if the variable is set. I've managed to do this by creating a file on Google Drive but it's too slow really.
I know about PropertiesService but according to the documentation this seems to only be specific to the current script or document. Is there any fast way to create a persistent variable accessible from anywhere in the google account via apps script?
There are several options
Use a project to create installable triggers instead of simple triggers,
Use a library,
Create a Workspace add-on
Create an Editor Add-on
On any of the above use the Properties Service and getScriptProperties instead of copies of the same script.
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.
I'm trying to find a way to run Google App Script code that's hosted on GHE. I found somewhere that you could get the script as a text string and use eval() to execute it as code, however I've read that it's a very unsecure way to do it and it shouldn't be done. Does anyone know any alternatives ways to run hosted code on Google App Script?
You could:
use this extension: https://chrome.google.com/webstore/detail/google-apps-script-github/lfjcgcmkmjjlieihflfhjopckgpelofo
use git and clasp CLI to push your code to your apps script project
From your specifications, I understand that you want to both host the script in GitHub Enterprise (GHE) and run it from there.
Even though Apps Script API offers a good range of functionalities such as executing functions from your App remotely it is not the best option for this scenario.
With clasp you can also write Apps Script functions remotely but eventually they have to be pushed to an Apps Script project.
So, after considering these limitations I think the best option you would have is to use the right Google API in your script (such as Gmail API for example). Its functional code can all be hosted and run from your application without the need of Apps Script projects and these APIs offer the same and extended functionalities than those offered by Apps Script.
I've published a standalone Google Apps Script project, and I include this code in 4 different scripts bound to 4 sheets. I develop locally using Clasp in combination with TypeScript, according to this setup.
The problem I experience is that sometimes when I push edits through Clasp my code in those standalone scripts (I never edit my library code) seems to remove the included library. Obviously, my code in the standalone scripts then fails to execute. I then have to manually re-add the library in all 4 bound scripts.
Development mode is turned off in all scripts.
One can include libraries in the appsscript.json file. This way, the library is properly attached to the code.
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