Button that performs a function (on slides) - google-apps-script

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");
}

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

Google script embedded in google sheet adding a menu item

What I tried is adding some functionality to my google sheet (creating events and pushing them to the google calendar). Everything works, but when I close the script editor my menu disappears.
I created the menu items like so:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar')
.addItem('Create Events Now', 'CreateEvent')
.addItem('Delete All Events in Calendar', 'DeleteAllEvents')
.addToUi();
}
That works just fine, but I would like to close the script and also when I share the spreadsheet, the member shall be presented with the menu to click and execute the functions. Anyone has a solution how to make that permanent? (without publishing the script which would be an overkill in my opinion)
In some cases OnOpen doesn't run automatically unless the editor has authorized the script. In this case, you need to give editors a way to trigger the script, so they may authorize it, before onOpen will run automatically. You can either give them instructions to do this manually through the script editor, or you can insert a "button" into the sheet.
To do this "button" - insert an drawing into the sheet (the "button"), bind the drawing to your script, and have first time users click the drawing.
In your spreadsheet, click "Insert" -> "Drawing"
Draw a "button" image with useful text for end users ("Show custom menu" or "No menu? Click here!", etc.)
Place the drawing on your spreadsheet in a visible/convenient location.
Click the "three dots" on the drawing and select "Assign Script"
Enter the name of the function (eg OnOpen)
If a user opens the sheet and doesn't see your menu, they can click this "button" to activate the script. They'll be prompted to authorise the script as needed, then the menu will show from that point forward. They should only need to click the button the very first time they open the sheet, unless the scopes change or they manually remove authorization in account settings.
onOpen is a reserved word for a function to be called automatically when a Google Sheets spreadsheet is opened by the spreadsheet owner or editors, it will not run for viewers.
You should check that in the project there isn't any other function named onOpen otherwise another function declaration could be executed instead of the one that you expect.
Reference
https://developers.google.com/apps-scripts/guides/triggers
The suggested answer from Cameron Roberts works as a workaround.
Although in my case the problem was that the script trigger for onOpen was missing. I had to edit the script trigger within the script I wrote. In the script editor go to "Edit" -> "Current projects triggers" and add a trigger for the onOpen function with an event "on open". Apparently that was missing in my case, after that edit, it worked like a charm.

GmailApp to .showModalDialog script in sheets?

I want to show the GmailApp 'create email' UI on a .showModalDialog method in Google Sheets.
So far, I have been able to create a mail merge macro that works perfectly fine with script editor & various HTML files. However, my next task is to allow the user to draft an email in Google Sheets within a .showModalDialog
Here is a screenshot from the popular MailMeteor, as to show you what I would like to emulate.
Show Modal Dialog
function showModalDialog() {
var html="";
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), 'Modal Dialog');
}
UI

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.

Attach a google form to a google spreadsheet

I'd like to create a form by google script.
This is pretty easy but I'd like to attached this form to a spreadsheet (no only the response), like I would have created it by the UI.
Is that possible ? Given the Formclass or FormApp is doesn't seem so. Is there a way around ?
EDIT: My goal was to create the form with the script and having the same result as if the user had had the form created from the UI interface (the main difference being having the form menu in the SS UI). It's apparently not possible.
You're right - it's pretty easy once you have the trick. Use FormApp to get the published URL of the form, UrlFetch to grab its html, then the HtmlService to present the form in the spreadsheet's UI.
See Single Google Form for multiple Sheets.