简体   繁体   中英

Enter key as well as submit button on Google Script Code in Sheet

I would like to be able to use enter as well as the submit button to execute / accept data entry and commit to cell.

I cannot seem to get the code to work. Any advice how to modify?

      <script>
         var itemBox = document.getElementById("itemname");
        document.getElementById("btn").addEventListener("click",addRecord);
        function addRecord(){

          var name = itemBox.value;
          if(name.trim().length == 0){
             M.toast({html: "Please enter a valid barcode!"})
          } else{

          var data = {
            name:itemBox.value
          };
          google.script.run.appendData(data);
          itemBox.value = "";
        }
      }
      </script>

Please read comments:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<script>
  var itemBox = document.getElementById("itemname");
  document.getElementById("btn").addEventListener("click",addRecord);
  function addRecord(){
    var name = itemBox.value;//item box is undefined
    if(name.trim().length == 0){
      M.toast({html: "Please enter a valid barcode!"})//M is undefined and toast does not exist clientside
    } else{
      var data = {name:itemBox.value};
      google.script.run.appendData(data);
        itemBox.value = "";
    }
  }
</script>
  </body>
</html>

Spreadsheet.toast() is a server side method of Class Spreadsheet and is not intended to function clientside on the browser.

Spreadsheet.toast()

So far your description of your question is incomplete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM