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.
Related
I need to get a Google sheet's cell background color hex code into that same cell.
This is the function I currently use:
function BGHEX(row, column) {
var background = SpreadsheetApp.getActive().getDataRange().getCell(row, column).getBackground();
return background;
}
Now I have 2 issues:
I need the function to run again when the cell's background color is edited. I know I need to use the installable trigger "onChange" but no idea how to set it up.
I need this script & function to run on a duplicate of this Google Sheet file (meaning other sheet id's).
Thanks in advance for your help!
Follow these steps to setup an onChange trigger in your spreadsheet
Open your Apps Script project.
At the left, click Triggers alarm.
At the bottom right, click Add Trigger.
Select and configure the type of trigger you want to create.
Click Save.
Your trigger setup should look like this:
To detect background change in your spreadsheet, you need to have an Event Object to determine which changeType the user made. For this scenario, we need 'FORMAT'.
Also, to change the background color of a cell or range, the user must click or highlight the range. Using getActiveRange() will help us determine which range is being edited.
Code:
function onBGChange(e) {
if(e.changeType == 'FORMAT')
Logger.log(SpreadsheetApp.getActive().getActiveRange().getBackground());
}
Example:
Here I change the background color of A1
Output:
Executions Tab:
To apply this to another spreadsheet, you need to replicate the code and trigger set up. A script is bound to the spreadsheet where it was created and functions like getActive() and getActiveRange() will only work on the spreadsheet the script was bound to.
References:
Installable Triggers
Event Object
getActiveRange()
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
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");
}
I have an issue with a google form I am working on. I set up the trigger as seen below.
When the form is submitted an alert box should pop up with information taken from a google sheet.(The form is linked to the sheet)
When I run the code from the script editor it shows the popup in the form, but the trigger never trips when I submit a response to the form.
I have tried:
deleting trigger
creating a new version of the script
adding trigger back
change trigger to TimeDriven and still no trigger
change the script to standalone with the same trigger
Not sure what the issue is since I can execute the code manually.
As Sandy Good states:
A dialog box can't be shown in "View" mode of a Google Form. When you are running the code from the script editor, the pop-up that you are seeing is probably in the "Edit" mode of the Google Form. "On Form Submit" doesn't run from Edit mode. If you are trying to get something to display to the user after they submit the Form, then your only option is the confirmation message: FormApp.getActiveForm().setConfirmationMessage(message)
Using .setConfimrationMessage(message) works for what I want to do. (Give a list of sheet cells when the response of the form is submitted)
I added a menu into my Google Sheets but can't find a way to delete it. The method used to add it was here.
Deleting the script didn't help, and neither did revoking access to my Google account.
What would I do to remove this menu from my menu bar so it no longer shows to the right of the "Help" menu?
The method you linked adds a menu when the Spreadsheet is opened. Have you closed the sheet and reopened it since deleting the onOpen() script? (and saving the script)
Without the onOpen() function in the bound script the menu will not be added the next time the Spreadsheet is opened.
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#addMenu(String,Object)