How do I get a sidebar button to execute a script in Sheets?

578 views
Skip to first unread message

Joshua Twiddy

unread,
Jul 21, 2023, 4:28:30 PM7/21/23
to Google Apps Script Community
Hey all!
I feel like there's something simple that I'm missing but I can't figure it out.

I want to execute a program that emails project assignments to people from a spreadsheet.  The program works, but to make it more user-friendly, I want to make a simple button within a Google Sheet that executes the program.

My Apps Script has two files:

Code:
function emailRequests() {
//Function Code
}

function onSubmitButtonClick() {
emailRequests();

function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Submit Requests', 'showSidebar')
.addToUi();
}

function showSidebar() {
var htmlOutput = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Requests');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
}

HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button onclick="onSubmitButtonClick()">Submit Requests</button>
</body>
</html>


The sidebar and button appear, but when I click on the button the console reads
Uncaught ReferenceError: onSubmitButtonClick is not defined at HTMLButtonElement.onclick

Emails are successfully sent when I run the emailRequests and onSubmitButtonClick functions independently.


Appreciate any help on why my button cannot find the function. Thank you!

cbmserv...@gmail.com

unread,
Jul 21, 2023, 5:22:25 PM7/21/23
to google-apps-sc...@googlegroups.com

The onSubmitButtonClick function needs to be in the client side domain and not in the server side code for it to be accessible by the button click.

 

So add the script tag:

 

<script>

.. put your function between these two tags.

 

Function will need to call the server function by using google.script.run.”serverfunctionName here”()

 

</script>

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/97ac5a8f-c79d-46db-8b97-9375b09b94b7n%40googlegroups.com.

Joshua Twiddy

unread,
Jul 24, 2023, 3:35:21 PM7/24/23
to Google Apps Script Community
Oh, thank you!  That worked perfectly.  That's helpful to know for a lot of future projects!

cbmserv...@gmail.com

unread,
Jul 25, 2023, 1:18:54 AM7/25/23
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages