skip to Main Content

I’m trying to call a function from a library in a HTML page but it doesn’t work :

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form id="myForm">
      <label for="champ1">Champ 1 :</label>
      <input type="text" name="champ1" id="champ1" required><br>
      <label for="champ2">Champ 2 :</label>
      <input type="text" name="champ2" id="champ2" required><br>
      <input type="submit" value="Valider">
    </form>

<script>
  var DATABASE_POST_TREATMENT_ID = '1HGHZ2xWdeN02vNOQ-XZyaoDMNxkIrz8ck5Psl5W3pdg';
  document.getElementById("myForm").addEventListener("submit", function(e) {
    e.preventDefault();

    // Récupérez les valeurs des champs champ1 et champ2
    var champ1 = document.getElementById("champ1").value;
    var champ2 = document.getElementById("champ2").value;
    
    // Exécutez votre fonction baseFinale avec les valeurs
  
      google.script.run.DatabaseposttreatmentMaster.coconexion(DATABASE_POST_TREATMENT_ID,champ1,champ2);
      google.script.host.closeDialog();

    // Réinitialisez le formulaire
    document.getElementById("myForm").reset();
  });
</script>
  </body>
</html>

I’ve got the following message in the console :

Uncaught TypeError: Cannot read properties of undefined (reading 'coconexion')
    at HTMLFormElement.<anonymous>

How can i solve the problem ?

2

Answers


  1. Try calling it by name through an intermediate function like this:

    function callLibraryFunctionsByName(funcname) {
      libraryName[funcname]();
    }
    

    Handling Arguments:

    Copy the following functions, paste and save and run testFunk the execution log is show below.

    function callFunctionByNameWithArguments(funcname,arguments) {
      this[funcname].apply(this,arguments);
    }
    
    function funkToCall(A,B) {
      console.log(A);
      console.log(B)
    }
    
    function testFunk() {
      callFunctionByNameWithArguments('funkToCall',[1,2])
    }
    
    Execution log
    9:46:14 AM  Notice  Execution started
    9:46:15 AM  Info    1
    9:46:15 AM  Info    2
    9:46:16 AM  Notice  Execution completed
    

    If your executing a library call then change this to the library name

    Login or Signup to reply.
  2. Read the library documentation that you’re trying to implement, there must be an installation page, normally it’s from npm install if your project is using Node.js or maybe there is a CDN https://cdnjs... make sure to added in your HTML script tag

    
    <body>
      <script src="https://cdnjs.your-javascript-code.min.js"></script> <!-- Replace it with your actual cdn url -->
    </body>
    
    

    hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search