The new Google Spreadsheets have add-ons like Google Analytics. They can generate an report using the GA API.
Now I need to programmatically trigger this add-on function. However the add-on is not a script with functions, but a black box it seems. There are scheduleable triggers to run my own defined functions, but I see no way to talk to the add-on and start the "get data" function it provides to the user (within the Spreadsheet UI).
Is there any way to run the add-on on a scheduled basis?
Unfortunately, I don't think so.
If methods in an add-on could be called programmatically, that add-on would be published as a Library. I did some digging and found the key of the Google Analytics add-on ("M6DHyOggPdtxTHKxmzonxqGg3No8Xsi1r"), tried to add it, and got the error:
A workaround might be to programmatically emulate a click on the appropriate menu item, but I don't believe that is currently supported by Apps Scripts.
You can't do this with the Google Analytics add-on. But the Supermetrics add-on also connects to Google Analytics and can be set to refresh with time-based triggers: https://chrome.google.com/webstore/detail/supermetrics/bnkdidgbiidpnohlnhmkehlimlnfhgce
Related
I'm involved in a standards development group where the meeting invitations are sent out, not as calendar invites, but as Word docs. I've written a script that allows me to take the Word doc uploaded to Google Drive, and run the script to create an entry on a Google calendar that I maintain.
At the moment, the script is unpublished, and to run it on the docs I use the "Test as Add-On" function from the script editor. That's a bit cumbersome because I have to go through the selection dialog to select the doc I want to run it on. Additionally, in the new scripts editor, it won't be possible to delete old tests and so my test dialog will end up getting filled up with every run of the script I've ever carried out.
It would be much better if I could have the script available to me, and me only, from the Extensions->Add-Ons menu when I open a doc. The script is not suitable for general publication: it works only on docs with the exact format of these meeting notices, so it should only be run by someone who knows what they're doing, and it accesses a calendar that I own, so I don't want to make it public. But I would like to be able to put it into some kind of "deployed" state so that, as I say, when I open any doc when I'm logged in as me, the script is just available directly from the Add-Ons menu.
Is there any way to achieve this? Or is there an alternative approach possible to this general problem, of having a script that you want to run on multiple documents that you own without having to formally publish it?
One option to "test as add-on" without publishing the script as an editor add-on, is to use installable triggers instead of simple triggers. This triggers should be created by using code in order to avoid having to add any code to each spreadsheet.
If you are determined to take the publishing path you can set your add-on listing to be "unlisted", this means that the add-on will not appear on the Google Workspace Marketplace, people will need to know the installation URL to be able to install the add-on.
Other options:
If you have a Google Workspace account you could publish the add-on privately for your domain. If you don't have this account type or if this is "not good enough", you might make your Google Apps Script add-on publicly installable but limit its core features to be used only by you by making use of a dynamic custom menu. For details please checkout Editor add-on authorization
The dynamic custom menu might based on a setting depending on the user email address. To make this work, as the Session.getActiveUser() requires authorization to run, it can't be executed by simple triggers, but you might implement a workflow to save it or a flag, i.e. add an option to the custom menu to initialize the editor add-on.
Another option is to make the dynamic custom menu based on the spreadsheet structure and/or content.
References
Installable Triggers
Enable and configure the Google Workspace Marketplace SDK
Related
Google Apps Script: Dynamically creating spreadsheet menu items
Publish an add-on privately
I have a published Editor Add-on for Google Sheets. It is deployed via the Sheets Add-on section within the App Configuration page of the Google Workplace Marketplace service on GCP.
I have been thinking about moving it to a Workspace Add-on in order to make use of the Card functionality. However, I haven't been able to confirm that the latter is actually able to use custom functions. For example, https://developers.google.com/apps-script/add-ons/concepts/types says:
Each Editor Add-on type (for example, Sheets add-ons) can have its own set of type-specific capabilities, restrictions, and special considerations. When building Editor Add-ons, it's important to understand these Editor-specific details
And going to the Sheets page, mentions custom functions. It also doesn't help that there seems to be no way to actually test the custom functions are working via either method of testing due to https://issuetracker.google.com/issues/36763437 without having a published Editor Add-on. So I haven't been able to confirm this way either.
Are Google Workspace Add-on a replacement to the current Editor Add-on? I.e. can I check/use the Google Workspace Add-on option and uncheck the Sheets Add-on? Or do I need to use both, the Workspace Add-on for the Card feature and Editor Add-on for custom functions?
So following this tutorial I am attempting to extend by adding the add-on via the sidebar. [See the screen shot below] []2
But I am getting this error when attempting to add this auth scope.
Error: invalid_scope
Some requested scopes were invalid. {valid=
[https://www.googleapis.com/auth/gmail.addons.current.message.readonly,
https://www.googleapis.com/auth/script.locale, https://www.googleapis.com/auth/spreadsheets.readonly,
https://www.googleapis.com/auth/gmail.addons.current.action.compose,
https://www.googleapis.com/auth/calendar.addons.execute,
https://www.googleapis.com/auth/drive.addons.metadata.readonly,
https://www.googleapis.com/auth/calendar.readonly,
https://www.googleapis.com/auth/gmail.addons.execute], invalid=
[https://www.googleapis.com/auth/spreadsheets.addons.execute]}
My question is do I need this https://www.googleapis.com/auth/spreadsheets.addons.execute for sheets to see the homepage triggers in my manifest? If not what am I overlooking?
UPDATE
We now have support for Workspace Add-ons in Google Sheets, Google Docs and Google Slides (though it may not be available in all regions as yet)...Google Forms is not yet supported.
What you want to do is not possible as you can only create Editor Add-ons for Google Sheets not GSuite Add-ons.
There are distinct differences between the two.
GSuite Add-ons are currently only applicable to:
Gmail
Google Drive
Google Calendar
Editor Add-ons are currently only applicable to:
Google Sheets
Google Forms
Google Docs
Google Slides
See add-on types documentation for clarification.
Actually you're experiencing this issue because the feature hasn't rolled out yet to your account.
It appears this is a Google Workspace account so you need to navigate to https://admin.google.com/ac/accountsettings/preferences
And select "New Feature"
Then you must select "Rapid Release" and the new Google Workspace add-on feature will be enabled for you probably within 15 minutes but it can take up to 24 hours.
I was experiencing this same issue and once I added this it worked fine.
Also, there isn't an auth scope of https://www.googleapis.com/auth/spreadsheets.addons.execute
You need to simply add https://www.googleapis.com/auth/spreadsheets
and it will work perfectly. Good luck!
I've been trying to create a Google Spreadsheet plugin from some existing Google App Scripts that I have, and one important part of this app script is Custom Functions.
Though the documentation for the add-ons doesn't indicate that this is supported, the documentation for Custom Functions does indicate that you can.
https://developers.google.com/apps-script/guides/sheets/functions
Through testing, I have not once been able to get Custom Functions exposed through a add-on. Does anyone know the secret sauce to get this to work?
Answer
According to Eric Koleda in [Code.gs - date_add_and_subtract]( https://github.com/google/google-apps-script-samples/blob/master/date_add_and_subtract/Code.gs) to make available the custom fuctions in an add-on it's required to include at least one add-on menu, but this is not working at this time on testing mode.
NOTE: The originally referred page was removed but the Date add and substract add-on sample code is available at https://github.com/googlesamples/apps-script/tree/master/sheets/dateAddAndSubtract.
The "solution" to test a custom function add-on is to publish the add-on privately so you could avoid the Google review of an untested add-on. Related Q&A: Publish an add-on privately
NOTES:
You have to create a Google Cloud Project, set OAuth Consent Screen, add the Google Workspaces Marketplace SDK, complete the configuration page, the listing page and to publish the add-on
Setting the OAuth Consent Screen for Internal Use requires to a Google Workspace Account
Setting the OAuth Consent Screen for External Use limited to test account only doesn't allow to publish the add-on to the GW Marketplace.
One scenario is to use different Google Cloud Projects for testing and production but use the same Google Apps Script project. Another scenario is to have two different Google Apps Script projects each one with their own Google Apps Project and someway copy the code from one to the other i.e. using Google Apps Script GitHub Assistant Chrome Extension, CLASP or the Google Apps Script API.
Remarks
There are a couple of related GAS issues, please star them:
"Test as add-on" from standalone project to Google Sheets doesn't link custom functions
After activating an add-on, custom functions get stuck Loading... until spreadsheet refresh
Excel supports the concept of a PERSONAL.XLS file which contains my personal scripts, which I want to be able to use on any spreadsheet/workbook that I open.
How do I do similar in Google Docs, i.e. have a set of scripts which, regardless of which Google Spreadsheet I open, will cause the onOpen event to be run and will thus add my extra menu?
You cannot do that in Google Spreadsheets. However the best practice is to
Write a standalone script with your onOpen and other functions.
Publish this as a library
Write a shell onOpen in each of the spreadsheets that you create
manually. :( This shell function will call the library's onOpen.
However, if you create a copy of a spreadsheet that has an associated script, then the copy will also have the script in it.
1) You can open your Script and from the Publish menu select "Test as add-on".
2) Now under "Configure New Test" you can select a document and then run the add-on for that doc.
Unfortunately you have to do this for each document one at a time. I would really love it if there was a way to tell a script to be available for all my spreadsheets, or at least have an easy way to install a personal add-on on a per-sheet basis just like you can install an add-on from the marketplace, kinda like a personal marketplace.
Although not recommended, You can also copy set of scripts to all other google spreadsheets programmatically using AppsScriptApi