sk65plus
sk65plus

Reputation: 179

Trying to validate input on sidebar against field on a spreadsheet using withFailureHandler

I am trying to validate user input against field on spreadsheet. Not sure if I am using withFailureHandler correctly. I have an output field defined in the form and this would show 'duplicate entry' if the entry is already present on the spreadsheet. I tried the following code for just testing to see if the form will display any output and this does not seem to be working.

code.gs

function sendItem() {
  var form = HtmlService.createHtmlOutputFromFile('test');
  SpreadsheetApp.getUi().showSidebar(form);
}

function getTest(sometext) {
  return false;
}

test.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div>
      <input type="text" id="amount" name="amount" placeholder="serving in gms"><br>
      <button class="myStyle" onclick="sendTest()">&#129746</button>
    </div>
    <div id="output" placeholder=" any errors will appear here"></div>
  </body>
</html>
<script>
  function sendTest(){ 
   var amount = document.getElementById("amount").value;
   google.script.run.getTest(amount)
   .withFailureHandler(onFailTest);
   document.getElementById("amount").value = "";
  }
  function onFailTest() {
    var output = document.getElementById("output");
    text = "duplicate entry retry";
    output.innerHTML = text;
  }
  </script>

Upvotes: 0

Views: 34

Answers (1)

Cooper
Cooper

Reputation: 64100

I'd do the html like this:

html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div>
      <input type="text" id="amount" name="amount" placeholder="serving in gms"><br>
      <button class="myStyle" onclick="sendTest()">&#129746</button>
    </div>
    <div id="output"></div>
  <script>
  function sendTest(){ 
   var amount = document.getElementById("amount").value;
   google.script.run
   .withFailureHandler(function(){output.innerHTML = "duplicate entry retry";})
   .getTest(amount);
   document.getElementById("amount").value = "";
  }
  </script>
  </body>
</html>

But I'd use withSuccessHandler to handle the getTest return onFailureHandler will handle errors not false returns, I think.

Upvotes: 1

Related Questions