Home > Blockchain >  Pass Button ID as argument in the executed function in Apps Script
Pass Button ID as argument in the executed function in Apps Script

Time:08-07

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'   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!

CodePudding user response:

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

const freezerButton = 'freezerButton';

CodePudding user response:

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

  • Related