0

I'm trying to mechanize calling a docs script routine from a button click (see this question). It seems the only way to do this is to create a sideboard for the document.

Here's my script:

function CreateSideBar ()
{
Logger.log ('CreateSideBar Entry')
var HTML = HtmlService.createHtmlOutput ('<button onClick="CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;
DocumentApp.getUi ().showSidebar (HTML) ;   
Logger.log ('CreateSideBar Exit')
}

function onOpen() 
{
CreateSideBar ()  
}


function CallFromSidebarButton ()
{
var ui = DocumentApp.getUi () ; 
ui.alert ('Call from sidebar OK') ;  
}

Things work fine if I call them from the script debugger, but if I open the document, the sidebar creates OK, but nothing happens when I click on the button.

Inspection shows:

userCodeAppPanel:1 Uncaught ReferenceError: CallFromSidebarButton is not defined
    at HTMLButtonElement.onclick (userCodeAppPanel:1)

The doc is shared here. You will probably need to be signed in and agree to a bunch of stuff.

1 Answer 1

2

To call a server side function from client side code you have to use google.script.run i.e. replace

var HTML = HtmlService.createHtmlOutput ('<button onClick="CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;

by

var HTML = HtmlService.createHtmlOutput ('<button onClick="google.script.run.CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;

Resources

Related

Sign up to request clarification or add additional context in comments.

1 Comment

Works a treat - many thanks for the answer and resources. I'm puzzled as to how the original code that doesn't work is able to work when executed in the debugger - is that not client-side also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.