google-apps-script restric menu use/creation to users - google-apps-script

I have a sheet with a google apps script that does various things. One of those things is building a menu on open.
I would like this menu to be built only when a certain specific user is acessing the sheet. How can I achieve this?

You cannot have restrictions inside onOpen since the user's email address is not available in any context that allows a script to run without that user's authorization. You can however include checks in the function that runs on clicking menu items.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Menu')
.addItem('First item', 'doSomething')
.addToUi();
}
function doSomething() {
if (Session.getActiveUser().getEmail() !== "abc#example.com") return;
// else do something here
}

Related

Create a Dialog box with custom buttons and Links

So I have an internal emailing list that I want to subscribe new employees to and unsubscribe employees that have left.
I'm using a google sheet with a check box to do this. Mainly because this sheet is already used in the HR process, so it's easy to remember to do.
When the check the box is checked I want a dialog box to pop up with a buttons that says
Subscribe and Unsubscribe. These buttons are linked to locations within the emailing tool that allows people to subscribe or unsunscribe.
The scribe I have at the moment is
var result = ui.alert
(
'What would you like to do?',
'Subscribe a new Employee or' + "\n" + 'Unsubcribe an employee thats left',
ui.ButtonSet.Subscribe_Unsubscribe
);
if (result == ui.Button.Subscribe)
{
}else
{
ui.alert('Employee has been removed from the internal mailing list');
}
}
I'm not sure how if by changing the button set names work this way and I dont know how to assign a link to the buttons either.
Looking forward to your responses!!!
Step 1:
First you would need to create an installable trigger that would execute a function in GAS whenever a cell is edited.
You can find an explanation / reference on Installable Triggers here:
Installable Triggers
Why installable triggers? Because you would need to connect to an email service, which requires authorization.
Sample Code:
/**
* Creates a trigger for when a spreadsheet is edited.
*/
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('myFunction')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Step 2:
To create custom dialog boxes you need to create the HTML file from scratch in GAS, the IDE allows you to create HTML files from the menu.
Then in your script you would need to use getUi().showModalDialog to display the dialog box.
I have some sample code in a Stack Overflow post here: Stack Overflow
Note that the HTML would need both onclick="google.script.run" and onclick="google.script.host.close()" parameters to execute functions within Apps Script.
References:
showModalDialog()
Templated HTML
Communication between HTML and Apps Script

Can I use Slides API to add an "Agree to Terms" dialogue before allowing viewer access to a Slides presentation?

I have a Google Slides presentation that I'd like to gate with a request to agree to "Terms & Conditions" before viewing. (The dialogue box would include an external link to the legal fine print.)
I see that with Google App Script adding a dialogue box is possible, but if one can be customized in this manner is unclear to me.
Thanks in advance for any help on this.
You want to open a dialog, when users open the Google Slides.
In this case, users are logged in to each Google account.
You want to make users show "Terms & Conditions" by the external link.
When users click "ok" button, when users reopen the Google Slides, you don't want to open the dialog.
When users click "cancel" button, when users reopen the Google Slides, you want to open the dialog.
You want to use a custom dialog for this.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and workarounds:
In order to open the custom dialog when users open the Google Slides, it is required to use the installable trigger. But at Google Slides, unfortunately, in the current stage, the OnOpen event trigger cannot be used as the installable trigger. By this, when users open the Google Slides, the custom dialog cannot be opened. It seems that this is the current specification. So in order to achieve abour your goal, it is required to think of the workaround. In this answer, I would like to propose the following 2 workarounds.
Workaround 1:
Fortunately, at Google Slides, the simple trigger can be used instead of the installable trigger which cannot be used. In this case, the OnOpen event trigger can be used as the simple trigger. So in this workaround, the built-in dialog is used with the simple trigger.
Sample script:
Please copy and paste the following script to the container-bound script of the Google Slides and save it. When you open the Google Slides, a dialog is opened. When "ok" is clicked, keyObject is set to the PropertiesService. By this, when you open the Google Slides again, the dialog is not opened. In this case, getUserProperties() is used. So keyObject can be used for each users.
function onOpen() {
var keyObject = {key1: "value1"}; // Here, key and value for checking whether user had clicked "ok" button.
var prop = PropertiesService.getUserProperties();
var value = prop.getProperty(Object.keys(keyObject)[0]);
if (value != keyObject.key1) {
var ui = SlidesApp.getUi();
var result = ui.alert('sample title', 'Please check this link.\nhttps://###/', ui.ButtonSet.YES_NO);
if (result == ui.Button.YES) {
ui.alert('"ok" was clicked.');
prop.setProperties(keyObject);
} else {
ui.alert('"cancel" was clicked.');
}
}
}
In this case, the URL cannot be clicked. So user is required to access to the URL. I think that this is the limitation when this workaround is used.
If you want to delete keyObject. Please use the following script.
function deleteProperty() {
var keyObject = {key1: "value1"};
var prop = PropertiesService.getUserProperties();
prop.deleteProperty(Object.keys(keyObject)[0]);
}
Workaround 2:
In this workaround, the built-in dialog and the custom menu are used with the simple trigger. In this case, the custom dialog is used.
Sample script:
Please copy and paste the following script to the container-bound script of the Google Slides and save it. When you open the Google Slides, a dialog is opened. The dialog shows "Please open dialog from the menu.". Users click "ok" button and open the custom dialog from the custom menu. When "ok" is clicked at the custom dialog, keyObject is set to the PropertiesService. By this, when you open the Google Slides again, the dialog is not opened. In this case, getUserProperties() is used. So keyObject can be used for each users.
function onOpen() {
var keyObject = {key1: "value1"};
var prop = PropertiesService.getUserProperties();
var value = prop.getProperty(Object.keys(keyObject)[0]);
if (value != keyObject.key1) {
var ui = SlidesApp.getUi();
ui.createMenu('Open dialog').addItem('Open dialog', 'openDialog').addToUi();
var result = ui.alert('sample title', 'Please open dialog from the menu.', ui.ButtonSet.OK);
}
}
function openDialog() {
var ui = SlidesApp.getUi();
var html = 'link<input type="button" value="ok" onClick="ok()"><input type="button" value="cancel" onClick="cancel()"><script>function ok() {google.script.run.withSuccessHandler(()=>google.script.host.close()).setProp()}function cancel() {google.script.host.close()}</script>';
ui.showModalDialog(HtmlService.createHtmlOutput(html), "sample");
}
function setProp() {
var keyObject = {key1: "value1"};
var prop = PropertiesService.getUserProperties();
prop.setProperties(keyObject);
}
In this case, the URL can be clicked. But in order to open the custom dialog, it is required to open it from the custom menu. I think that this is the limitation when this workaround is used.
Note:
Above scripts are the simple scripts for testing the workaround. So please modify them for your actual situation.
References:
Simple Triggers
Installable Triggers
Alert dialogs
Custom dialogs
If I misunderstood your question and this was not the direction you want, I apologize.

Button that performs a function (on slides)

I saw that in the spreadsheet you can create a drawing and link to a function in the script.
I need to: Create a button or link where clicking it will perform a function in the script.
It will be a simple email sending function.
Example: When the user clicks the button or link in the presentation, they receive an email from them.
There isn't a way to assign a script to a button in Google Slides in the same way as there is in Google Sheets.
The only alternative would be to use either a simple or installable trigger depending on your needs.
Google Slides only has compatibility with the onOpen() and onInstall() simple triggers which you can see here, though you would be able to send the mail with the built-in Apps Script library as such:
function sendEmail() {
MailApp.sendEmail("username#domain.com", "subject", "body");
}

Google Apps - Script - Side bar OnOpen/OnInstall function

I'm recently having issues with Google Apps Scripting, I have a script which installed domain wide, which is enables users to make some tasks easier with the side-bar menu however from last week side-bar menu is not popping up anymore. Nothing has been changed on the code.
I did some workaround and tried all possible ways - deauthorization \ authorization, deploying the same code with different name, nothing worked except if the user removes it and adds it to the spreadsheet which they work - it's popping up and working only for that sheet and only for one time.
Can you please advise, how to fix this issue?
Here is my OnInstall and OnOpen functions:
function onInstall(e)
{
onOpen(e)
}
function onOpen(e) {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
Thanks in advance.
Edit : Triggers making the side-bar work on simple scripts on spesific spreadsheets, but this method is not working on domain-wide installed add-ons.
It should be possible to add a trigger to run the custom function which shows the SideBar.
I have a function like the following in my code that runs on my domain:
function createOnOpenTrigger()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
// Trigger on open
ScriptApp.newTrigger(FunctionToRunOnOpen)
.forSpreadsheet(sheet).onOpen().create();
}
function FunctionToRunOnOpen()
{
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
You could then put the createOnOpenTrigger function as an option in your custom menu - to be run only once everytime a copy is made of the entire spreadsheet.

Showing sidebar for viewers

How can I show the sidebar for anonymous viewers (or editors)?
I tried using normal and installable triggers:
Normal Triggers:
function onOpen(){
var html = HtmlService.createHtmlOutputFromFile('Page')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
This worked with the owner of the spreadsheet but didn't work with anonymous users although anyone with the link has editing permission.
Installable Triggers:
function showSidebar(){
... the same body of the previous 'onOpen' function
}
Then, I bound the function showSidebar to an installable trigger that is called when the spreadsheet is opened.
This didn't work with either the owner or an anonymous user!
Finally, I tried binding the function showSidebar to an image inserted into the spreadsheet but didn't work with an anonymous user. It displays a message saying
Script showSidebar experienced an error
and even if the final method worked well, it will not show the sidebar automatically.
It looks to me that the code is almost the same of https://developers.google.com/apps-script/guides/dialogs#custom_sidebars. That has two files, Code.gs and Page.html. As the OP didn't mentioned the code of Page.html it's very likely that it was missing on his attempt as using the same code as the OP worked just fine if the code of the Page.html file is include and the file is opened by the owner of by an editor.
Regarding running as anonymous user, the onOpen doesn't run when the spreadsheet is opened by an anonymous with view or edit access. There is a report related to this in the Google Apps Issue Tracker
Issue: 5747 Trigger for anonymous user / script for anonymous user
UPDATE:
From an answer by +Samantha to a similar thread in the Google Docs Help Forum
In order for Scripts to run on a Google Sheet, the user must be logged
in and have "can edit" rights. This means that anonymous users will
not be able to run a Script.
If you would like to see this functionality added to Scripts, I
recommend navigating to the Google Developers' Apps Script support
page and pressing the "Send Feedback" button.
Code from the first link
Code.gs
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
Page.html
Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />