google.script.run does not work from HTML template

9.2k Views Asked by At

I am developing spreadsheet tools from a library, through an HTML template, but the server functions do not work.

gs simplified code (the functions are longer):

function Cancel_Version(){
    
    return Action_Box("¡HEY!","You're to cancel active edition.\n\n Sure?","Cancel_Version2()");

}

function Action_Box(TITLE,MESSAGE,FUNCTION){

   var html = HtmlService.createTemplateFromFile('ACTION_BOX');
   html.data = MESSAGE;
   html.action = FUNCTION;
   SpreadsheetApp.getUi().showModalDialog(html.evaluate().setHeight(200),TITLE);

}

function Cancel_Version2(){

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.deleteActiveSheet();

}

HTML code:

<!DOCTYPE html>

    <?!= include('CSS'); ?>
    
  <form style="font-size: 22px; font-family: 'calibri'; ">
        
        <?!= data; ?>
        <br>
        <br>
        <input type="button" value="ACCEPT" onclick="<?!= action; ?>; google.script.host.close();"/>
        <input type="button" value="CANCEL" onclick="google.script.host.close();"/>
      
  </form> 

Why the code action does not work??. Even I have substituted <?!= action; ?> with Cancel_Version2() but still does not work. On the other hand, if I call directly the function from a onOpen menu, it works.

What am I missing???

2

There are 2 best solutions below

6
Senor Penguin On

Where is your script? You need to include the google.script.run inside a script in the HTML.

Info here
And here

Try something like this:

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

function Cancel_Version2(){
  var ui = SpreadsheetApp.getUi();
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  ss.deleteActiveSheet();
}

index.html

<?!= include('CSS'); ?>

<form style="font-size: 22px; font-family: 'calibri'; ">
<?!= data; ?>
<br>
<br>
<input type="button" value="ACCEPT" onclick="deleteSheet()"/>
<input type="button" value="CANCEL" onclick="google.script.host.close();"/>
</form> 

<script>
  function deleteSheet() {
    google.script.run.Cancel_Version2()
  }
</script>

You can also run it with a success or failure handler to get a call back function. Where onSuccess() & onFailure() would be response functions of the first function you are calling.

<script>
function onSuccess() {
  //create onSuccess response function
}

function onFailure() {
  //create onFailure response function
}

function deleteSheet() { 
  google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).Cancel_Version2()
}
</script>
0
Wicket On

On the Action_Box function of the .gs file, replace

html.action = FUNCTION;

by

html.action = 'google.script.run.';
html.action += 'withSuccessHandler(google.script.host.close).'; 
html.action += FUNCTION;

On the ACTION_BOX.html file instead of

<input type="button" value="ACCEPT" onclick="<?!= action; ?>; google.script.host.close();"/>

use

<input type="button" value="ACCEPT" onclick="<?!= action; ?>;"/>

Related