Deploying and Using a Google App Script - google-apps-script

I followed the app script guide to create a menu for my spreadsheet.
I was tested during development in the spreadsheet running it by going to:
Tools -> Script manager...
But now I want to permanently add it to the spreadsheet, but I don't know hot to deploy it.
I deployed it as a webapp, but when I click the test link i get:
Script function not found: doGet
I can not find documentation to complete that doGet function.
Any help on the last step to use my first Google App Script?

if the function that create your menu is called onOpen(e) it should automatically do the job for you when the document is opened, you don't need to publish the app or anything like that.
But in order to be executed the onOpen function must not call personnal data for the user this function onOpen (when triggered automatically, when you open the sheet) has limited rights:
in the doc https://developers.google.com/apps-script/guides/menus,
it say:
Warning: Note that the add-on menu sample above compares the
e.authMode property to values from the ScriptApp.AuthMode enum. This
is because an add-on that has not been used in the current document
runs its onOpen(e) function with severe restrictions, as explained in
the guide to the add-on authorization lifecycle. If your published
add-on fails to add its menu items, look in the browser's JavaScript
console to see if an error was thrown, then examine your script to see
whether the onOpen(e) function or global variables call services that
aren't allowed in AuthMode.NONE.

Related

Google Apps Script PropertiesService Permissions for AddOns

I'm currently developing an Editor Add-on for Google Sheets using Apps Script. There's an onOpen function that sets up the menu items.
I also have a CONFIG variable in the root (not in any function) like this:
const CONFIG = {}
function setProperties_(){
CONFIG.tmpSheetId = PropertiesService.getScriptProperties().getProperty('TMP_SHEET_ID');
}
setProperties_();
If I run any functions from within the script editor, everything runs fine. No issues. However, when I do a test deployment I get the error below from the moment the onOpen() runs:
You do not have permission to call getScriptProperties
I've tried adding various script scopes from here but nothing works.
When you first install the add-on (the entry for the add-on becomes visible in the sheet menu), it runs in AuthMode.NONE that allows you to inspect only the current user's locale. Please refer to the table here Google Add-On Authorization Lifecycle
As Cooper pointed out, calling setProperties_() in global scope occurs before your add-on is authorized by the user. Move the function call to a nested function and make sure you are through with the authorization flow first.

How to Test OnOpen() menu populate in Apps Script Sheets Add On?

For months I'm trying, without success, to submit my Google Sheets Add-On to be available at Google Workspace Marketplace. On the last tentatives I'm receiving this message:
The App doesn’t meet the publishing review criteria on the following:
Menu - Menu options not shown after App is installed. Please ensure that the add-on correctly uses onInstall() and onOpen() to populate its menu. The menu items populate when the add-on is first installed and when a different file is opened. See Editor add-on authorization.
My question is: How can I simulate the installation by myself in order to detect what function or variable is causing the problem?
I already try to avoid declaring Global variables, tried zillions of different ways to populate the menu, but it seems that nothing work. The documentation of Google Apps Script is too generic, the people at GWM doesn't show what error is happening. I really doesn't know what can I do.
How can I avoid a situation that I cannot replicate?
If I just have access to the my own add on GWM page to test it.
Sometimes I ask myself if Google is interested in have third party add on. It's so hard to have information.
Any help will be welcomed.
This is my initialization routine right now:
var ui = function(){
return SpreadsheetApp.getUi()}
/**
* #OnlyCurrentDoc
* ver: https://developers.google.com/apps-script/guides/services/authorization
*/
// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen(e) {
ui().createAddonMenu()
.addItem('Importar fontes', 'ExtrairComentarios')
.addSeparator()
.addItem('Relação de fatores', 'doFactors')
.addItem('Análise simples', 'doTable')
.addItem('Georreferenciamento', 'doMap')
.addSeparator()
.addItem('Gera matriz multimodal', 'GeraMatrizMM')
.addItem('Análise multimodal', 'doMultimodal')
.addItem('Gráfico multimodal', 'doGet')
.addItem('Gráfico multifatorial', 'doMultifatorial')
.addSeparator()
.addItem('Tutorial', 'openTutorial')
.addSeparator()
.addItem('Versão atual', 'DoVersaoAtual')
.addItem('Start!', 'DoStart')
.addToUi();
// Script external request
//eval(UrlFetchApp.fetch("https://cdnjs.cloudflare.com/ajax/libs/alasql/0.6.2/alasql.min.js").getContentText())
}
/**
* The event handler triggered when installing the add-on.
* #param {Event} e The onInstall event.
*/
function onInstall(e) {
onOpen(e);
}
P.S. Believed me, I already spend months trying to solve this.
Google recently released a way to test Editor add-ons and updated the docs. See https://developers.google.com/apps-script/add-ons/how-tos/testing-editor-addons
Now it's possible to create a test deployment for Editor add-ons. Please checkout the details on the previous link.
If you have a Google Workspace account, an alternative might be to publish the add-on for internal use only for testing purposes. Once you have it working you might make a copy to publish publicly, and keep the internal use only for testing future changes.
To prevent having problems use the simple onOpen trigger only to show the custom menu. If you want that the add-on does something else on open, do that using an installable trigger, this imply to add a way for the end-user to create this trigger, i.e. include a option on the custom menu for that.

SpreadsheetApp.getActive() fails in onOpen trigger in Google Sheets addon

I would like to read the Active spreadsheet and add menu items based on that. Below is the code in OnOpen(e) trigger:
function onOpen(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
//add menus based on values of sheet
}
I am getting below exception:
[Exception: You do not have permission to perform that action.]
I have gone through the documentation and found that the AuthMode is None in OnOpen trigger which does not allow access to any services that require authorization.
Please suggest if there is any other way to accomplish my task.
Thanks.
It's not possible to access the spreadsheet unless the owner or editor of the spreadsheet uses the add-on in that spreadsheet. As written in the official document,
However, because an editor add-on automatically runs its onOpen(e) function to add menu items when a document opens, the behavior above adds some complexity to Apps Script's authorization rules. After all, users wouldn't be comfortable with an add-on accessing personal data every time they open a document.
The add-on needs to be enabled for the document for the addon to get access to the document. When it's enabled, onOpen() runs in AuthMode.LIMITED, where you get access to the bound document. This "enabled" state is caused by
Getting an add-on from the store while using that document, or
Using a previously installed add-on in that document
In all other cases, add on runs in AuthMode.NONE. In this mode, You are only able to add menu items without access to any data including the bound spreadsheet. Add Menu items to request access to such data. Then, after getting AuthMode.FULL, the rest of the workflow can be done.
References:
The complete lifecycle

When Google document is copied with container-bound script, how to get its installable triggers?

Issue
I have a container-bound script attached to my document.
It has onOpen installable trigger and I need to get the script in newly created documents.
The most effective way I found is to create the new documents by copying from the original document.
The problem is, that the triggers are not copied with it.
Conditions:
Simple triggers are not enough, I need to use action requiring authorization
I don't want to force my users to create manually trigger for each document
I don't mind if the installable trigger is created after clicking button from the programmatically created menu
My thoughts:
I wanted to create it programmatically – there is a problem with the testing environment. I am getting an error:
The add-on attempted an action that is not permitted in Test as add-on
mode. To use this action, you must deploy the add-on.
as I understood, I need to release the project to the store to use this, which I don't want to do.
I don't mind release as an add-on, but the google script IDE only offers to release as web Docs web add-on.
Code:
function onOpen(e){
DocumentApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Custom menu')
.addItem('Open sidebar automatically', 'createTrigger')
.addToUi();
}
function createTrigger() {
var doc = DocumentApp.getActiveDocument();
ScriptApp.newTrigger('onOpenReal')
.forDocument(doc)
.onOpen()
.create();
}
function onOpenReal(e){
...something requiring authorization...
}
I figured the answer partially:
I think it is not possible to copy the document together with its triggers, but I didn't find any official documentation about that.
But the error:
The add-on attempted an action that is not permitted in Test as add-on
mode. To use this action, you must deploy the add-on.
in the seemingly regular environment was caused by saved test configuration in the Google apps script editor.
To get rid of the error you need to first delete all test configurations in "Test as add-on" option. Until that the system sees all launches as "test" even if opened as a regular user.

Spreadsheet Editable only with HTML Service gui?

I have a Google App spreadsheet that I would like to make available to anyone with the link. It uses a custom gui to edit the cells of the spreadsheet. The custom gui is a html file launched using the HtmlService class in the Google Apps Script file.
How can I adjust the settings of the sheet so that anyone can open the spreadsheet and use the custom gui to make changes to the spreadsheet, but not make changes through any other means?
Here are the results I've had under various scenarios.
The gui works as expected but the sheet can be modified without it:
Anyone with link can edit
Gui launched via custom menu (App script runs as user)
The gui works as expected but the sheet can still be modified without it:
Anyone with link can edit
Gui launched via installable trigger (App script runs as trigger owner, which is also the document owner)
The gui cannot edit the sheet and neither can users otherwise:
Anyone with link can comment
Gui launched via installable trigger (App script runs as trigger owner, which is also the document owner)
My thought was that since the installable trigger is running as a user with permissions to change the sheet, that would work. Does that trigger's permissions not extend to a HTML Service gui that it opens?
Its not related to htmlServices. Simply any gui that you launch by any means from the spreadsheet will run as the user using the spreadsheet.
You need to publish the service (to run as the script owner) and use that gui through its service url, NOT launching it from the spreadsheet (menu etc).