0

In my apps script addon there is a lot of buttons and I want to pass the button ID as variable in the function executed. I have this execFunction in code.gs to avoid google.script.run duplicates, works well without the btnID parameter, but it doesnt let me pass an argument to the final function. fa seems not valid.

What could be the right path to have the possibility of make if/else depending on the clicked button id?

index.html

<button id='freezerButton' class="btn btn-primary"  onclick="execFunction('myFunction', this.id)">FREEZER</button>

<script>
    function execFunction(functionName, btnID) {
      google.script.run[functionName](btnID);
    }
</script>

code.gs

function myFunction(btnID) {

  if (btnID == freezerButton) {
    Logger.log('From freezerButton')
  }

}

Thanks!

alsanmph
  • 77
  • 5
  • 1
    Is the variable `freezerButton` defined in `code.gs`? What is `fa` and what do you mean by `fa seems not valid.`? – TheMaster Aug 06 '22 at 17:03

2 Answers2

0

Instead of passing this.id on the onclick attribute, pass this.

Example:

code.gs

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

function myFunction(id){
  console.log(id)
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button id="myButton" onclick="execute('myFunction',this)">Click me!</button>
    <script>
      function execute(name,btn){
        google.script.run[name](btn.id)
      }
    </script> 
  </body>
</html>

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Why doesn't/wouldn't `this.id` work? – TheMaster Aug 06 '22 at 16:52
  • @TheMaster I didn't said that... anyway, I realized the OP problem is on the if statement expression (posted a new answer, I might delete this one later) I saw your comment on the question after I posted the new answer) – Rubén Aug 06 '22 at 17:06
0

Replace freezerButton by 'freezerButton', or before the if statement, declare freezerButton assigning the appropriate value, i.e.

const freezerButton = 'freezerButton';
Rubén
  • 34,714
  • 9
  • 70
  • 166