1

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???

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • See [this thread](http://stackoverflow.com/questions/38927706/google-script-host-close-to-close-a-dialog-box-not-working) – Karl_S Mar 06 '17 at 20:35
  • 1
    The scriptlet `!= action; ?>` is not referring to a function. So, if you are trying to return a function name from the server, that isn't going to work. You can get a global variable from the server. But I don't see any global variable named `action` in your server code. If what you are trying to accomplish, is to be able to call different server functions from the same button, what is the criteria for changing the function name? You could use the same server function name: `google.script.run.alwaysTheSameFunctionName(different value)` but send a different value, and cause that function – Alan Wells Mar 06 '17 at 20:47
  • to branch to other different functions depending upon the value passed to the server. You could run a function on the server that returns the correct google.script.run text: `!= serverFunctionToGetCorrectCode() ?>` Then in .gs code: `function serverFunctionToGetCorrectCode() { if (1===1) {return 'google.script.run.myServerFunctionOne()';}}` – Alan Wells Mar 06 '17 at 20:51
  • Hi Sandy. Thank for answering. You are right, my intention is to pass different functions as plain text. through 'action' variable. Anyway, forget about this, if I get ride of this and place directly the function name, it still fails. Why? – Jose Antonio Fernandez Mar 06 '17 at 21:34

2 Answers2

0

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>
Senor Penguin
  • 385
  • 1
  • 5
  • 17
  • Hi Senor Penguin. I have already tried this, but nothing happends. The thing is that I want to use 'HtmlService.createTemplateFromFile' istead of 'HtmlService.createHtmlOutputFromFile' because I'd like to use the same template for different titles, prompts and actions for the html dialogs I show, so I need to evaluate the html. – Jose Antonio Fernandez Mar 07 '17 at 19:34
  • One thing to have into account is that all code is in a library and I'm executing from an spreadsheet. Does this have something to do with the failure? In that case it is strange as if I execute the function directly from a custom menu it works perfectly. – Jose Antonio Fernandez Mar 07 '17 at 19:40
  • You need to put the `google.script` inside of `` to call a function from a google script file for the HTML Service to work. `onclick="!= action; ?>; google.script.host.close();"/>` doesn't do anything. – Senor Penguin Mar 07 '17 at 19:59
  • Sorry to disagree but it does. Now I have test another thing and it works: I have placed all the code (gs and html) as bonded in the spreadsheet and it runs perfectly!! At the end I came across that the problem is that the code was in a library. The thing is that I do not understand why, as at the beginning all was ok when using ui alerts. Any ideas??? – Jose Antonio Fernandez Mar 07 '17 at 20:21
  • Why identical code works if bounded but not if executed from a library??? – Jose Antonio Fernandez Mar 08 '17 at 18:10
  • 1
    Nothing on this? Please help!! – Jose Antonio Fernandez Mar 27 '17 at 12:00
0

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

Rubén
  • 34,714
  • 9
  • 70
  • 166