Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick function is not being invoked in Google App scripts

I'm creating a script that can send emails to the selected people in which the checkbox is set to true and I'm creating custom sidebar for it. I have made the script successfully. But the sidebar is not working I have tried everything like creating an eventListner or adding a onClick event but still my function is not invoked I have checked in logs it is not invoking at all. Please could anyone tell what's wrong with my code?

This is my HTML file

<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
      <div>
    <div class="form-group">
      <label for="enter-subject">Subject</label>
      <input type="text" class="form-control" id="enter-subject" aria-describedby="emailHelp" placeholder="Enter Subject">
    </div>
    <div class="form-group">
      <label for="enter-message">Message</label>
      <input type="text" class="form-control" id="enter-message" placeholder="Enter Message">
    </div>
    <button class="btn btn-primary" id="mainButton" onClick="afterButtonIsClicked">Submit</button><!-- 
 onClick="afterButtonIsClicked();" -->
  </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    <script type="text/javascript">
    function afterButtonIsClicked(){
      var subject = document.getElementById("enter-subject")
      var message = document.getElementById("enter-message")
      var rowData = {subject: subject.value, message: message.value}
      console.log(rowData)
      google.script.run.addNewRow(rowData)
    }
    
//    document.getElementById("mainButton").addEventListner("click",afterButtonIsClicked)
    </script>
  </body>
</html>

This is my script file

function addNewRow(rowData) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  
  var dataRange = sheet.getRange(startRow, 1, sheet.getLastColumn(),sheet.getLastColumn());
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var checkbox1 =row[1];
    var emailAddress = row[0];
//    var subject = "test subject";     
//    var message = "test message";    
    if(checkbox1) { 
      console.log(`${rowData.subject} ${rowData.message}`)
      MailApp.sendEmail(emailAddress,rowData.subject,rowData.message)
    }
  }
}

this is my UI for sidebar file

function loadForm() {
  const htmlForSidebar = HtmlService.createTemplateFromFile("index")
  const htmlOutput = htmlForSidebar.evaluate()
  
  const ui = SpreadsheetApp.getUi();
  ui.showSidebar(htmlOutput)
}

function createMenu() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu("My Form")
  menu.addItem("Show Form", "loadForm")
  menu.addToUi();
}

function onOpen() {
  createMenu()
}

Could anyone diagnose and tell me the error?

like image 219
Devyash Bordia Avatar asked Jun 30 '20 20:06

Devyash Bordia


1 Answers

This should fix the onClick issue:

onClick="afterButtonIsClicked();"
like image 63
beskgar Avatar answered Oct 15 '22 15:10

beskgar