Hello to the entire community and thank you for your help.
I have the following problem:
I have created a script in a Sheet that checks the existence of a data in a column. This script is part of the Sheet menu, it has its js code and its html part. It works like this: it looks in the sheet if there is a DNI number, if it is, it returns an alert indicating that it is already entered, otherwise it loads the data in the Sheet.
Inside the Google Sheets menu it works perfectly, but if I implement it as a web app it doesn't work. What's more, if I implement the functions separately (search / load) they work perfectly, but when combining them they don't. The form is displayed on the page but when I click the button the page goes blank. I suspect there is something I am missing from the "google.script.run" but I can't figure out what it is.
I leave the code so that you can help me. I appreciate your help in advance!
-------------------------------------------------------------------------------------------------------------------------
// GS-----------------------------------------------
function grabar(dni,ape,nom,fechaN,mail){
var dataPer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DatosPersonales");
dataPer.appendRow([dni,ape,nom,fechaN,mail]);
}
function validadorMatricula(dni){
var datosPer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DatosPersonales");
var dnilocal = datosPer.getRange("A1:A").getValues();
var contador = -1;
var extension = datosPer.getLastRow()
while (contador<= extension+1){
contador++
if(dnilocal[contador][0]!== dni && contador<= extension) contador++;
if (dnilocal[contador][0]!== dni && contador== extension+ 1) return true;
if (dnilocal[contador][0] == dni) throw ("error");
}
//Html----------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form name="formu" id="formu"method="post">
<label>Apellido: <input type="text" name="ape" id="ape" autocapitalize="words" class="validate" required="true"></label><br><br>
<label>Nombres: <input type="text" size="40"name="nom" id="nom"autocapitalize="words" required="true"></label><br><br>
<label>N° de documento: <input type="number"name="dni" id="dni" min= "1000000" max="99999999"required="true"></label><br><br>
<label>Fecha de nacimiento: <input type="date"name="fechaN" id="fechaN"></label><br><br>
<labe>Correo electrónico: <input type="email"size="40"name="mail" id="mail"required="true"></label><br><br><br><br>
<input type="submit" name="Cargar" id="Cargar"value= "Cargar">
</form>
<script>
document.getElementById("Cargar").addEventListener("click", enviarDNI);
function enviarValores(){
var ape = document.getElementById("ape").value;
var nom = document.getElementById("nom").value;
var dni = document.getElementById("dni").value;
var fechaN = document.getElementById("fechaN").value;
var mail = document.getElementById("mail").value;
var apeValido = document.getElementById("ape").checkValidity();
var nomValido = document.getElementById("nom").checkValidity();
var dniValido = document.getElementById("dni").checkValidity();
var mailValido = document.getElementById("mail").checkValidity();
if ((apeValido) && (nomValido)&&(dniValido)&&(mailValido)){
google.script.run
.withSuccessHandler(cargaExitosa)
.withFailureHandler(noCargo)
.grabar(dni,ape,nom,fechaN,mail);
}
}
function alertaNoexiste(){
alert("El estudiante ya está ingresado.");
formu.reset()
}
function cargaExitosa(){
alert ("La carga se ha realizado con éxito")
formu.reset();
}
function enviarDNI(){
var dni = document.getElementById("dni").value;
google.script.run
.withSuccessHandler(enviarValores)
.withFailureHandler(alertaNoexiste)
.validadorMatricula(dni);
}
function noCargo(){
alert("No se pudo cargar el alumno");
}
</script>
</body>
</html>