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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
rossmcm
  • 5,493
  • 10
  • 55
  • 118

1 Answers1

1

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 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? – rossmcm Mar 07 '21 at 03:32