How to prevent validation based on latest action

Hi everyone,
I need some help. I am by no means a programmer :melting_face:, but following tutorials I am trying to put together a form with WebApp for check out/in and reserving tools.

The trouble I have is that I would like to prevent tools that have been already checked out from being check out again. Similarly, I want to avoid having tools reserved when they have been already checked out.

Now, I’ve managed to block double check-outs, but I can’t prevent the reservations and I need help with that.

<!DOCTYPE html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Geräte Management System</title>
    <base target="_top">    
    <!-- Metro UI -->
    <link rel="stylesheet" href="">
    <link href="" rel="stylesheet">



 <!-- HEAD HTML -->

<!-- BODY HTML -->
  <div class="container"> 
    <h1 class="brand">Geräte & Autos Management System</h1>            
    <div class="wrapper animated bounceInLeft">
      <div class="company-info">
        <h3>AAB Archäologie</h3>        
          <li><span class='mif-home fg-darkTeal'></span> company address</li>
          <li> <span class='mif-phone fg-darkTeal'></span>company email address</li>
          <li><span class='mif-envelop fg-darkTeal'></span>company phone</li>
      <div class="main">
        <h3>Geräte aus- oder einchecken</h3>
        <form id="main-form">       
          <div class="form-group">
            <input type="text"  placeholder="Nachname" id="lname" data-prepend="<span class='mif-user fg-darkTeal'></span>"><br>
          <div class="form-group">
            <label>Persönlicher Code</label>
             <input type="password"  data-role="big-input" placeholder="Code fĂĽr den eingefĂĽgten Nachnamen" id="persid" data-prepend="<span class='mif-key fg-darkTeal'></span>"><br>

          <div class="form-group">
            <input type="text" data-role="big-input" placeholder="Gerätename (wie auf dem Etikett)" id="gerid" data-prepend="<span class='mif-wrench fg-darkTeal'></span>"><br>

          <div class="form-group">          
            <textarea data-role="big-textarea" placeholder="relevante Kommentare über die Geräte" id="kommentar"data-prepend="<span class='mif-paragraph-left fg-darkTeal'></span>" rows="7"></textarea><br>

          <div class="form-group">
            <button id="checkOut" type="submit" data-action="Check OUT">Check OUT</button>
            <button id="checkIn" type="submit" data-action="Check IN">Check IN</button>
            <button id="reserviert" type="submit" data-action="IN Reserviert">Reserve</button>                
          <div id="message" class="d-none"> 


<!-- SCRIPT .JS -->
    <!-- Metro UI -->
    <script src=""></script>

      const CheckInOutApp = {}
       CheckInOutApp.onLoad = function () {
        CheckInOutApp.form = document.getElementById("main-form")
        CheckInOutApp.lnameInput = document.getElementById("lname")
        CheckInOutApp.persidInput = document.getElementById("persid")
        CheckInOutApp.geridInput = document.getElementById("gerid")
        CheckInOutApp.kommentarInput = document.getElementById("kommentar")
        CheckInOutApp.checkOutButton = document.getElementById("checkOut")
        CheckInOutApp.checkInButton = document.getElementById("checkIn")
        CheckInOutApp.reserviertButton = document.getElementById("reserviert")
        CheckInOutApp.message = document.getElementById("message")

        CheckInOutApp.form.addEventListener ("submit",CheckInOutApp.onSubmit)

      } // CheckInOutApp.onLoad function 

      CheckInOutApp.onSubmit = function(e){
        console.log("Form Submitted")
      } //CheckInOutApp.onSubmit function 

      CheckInOutApp.checkInOut = function(e){
        const management = {
                           lname: CheckInOutApp.lnameInput.value,
                           persid: CheckInOutApp.persidInput.value,
                           gerid: CheckInOutApp.geridInput.value,
                           kommentar: CheckInOutApp.kommentarInput.value,
                           } => {
          lname: CheckInOutApp.lnameInput.value = ""
          persid: CheckInOutApp.persidInput.value = ""
          gerid: CheckInOutApp.geridInput.value = ""
          kommentar: CheckInOutApp.kommentarInput.value = ""
        }).withFailureHandler(() => {
          setTimeout(() => {CheckInOutApp.message.classList.add("d-none") },3000)

      } // CheckInOutApp.checkInOut function



function doGet() {
return HtmlService.createHtmlOutputFromFile ("Form")

//function acceptData(formData){

function checkInOut(management){
  // const management = {
   //                  lname: "Savu",
   //                  persid: "5584",
   //                  gerid: "Ray",
   //                  action: "Check OUT"
  //                   }

  if(!["Check OUT","Check IN"].includes(management.action)){
    throw new Error ("Check IN or Check OUT Failed")

  console.log("Initial check passed")

  const ss = SpreadsheetApp.getActiveSpreadsheet ()
  const wsData = ss.getSheetByName("Data")
  const wsPersonal = ss.getSheetByName("Personal")
  const personalData = wsPersonal.getRange(2,1,wsPersonal.getLastRow() -1,2).getValues()

  const matchingPersonal = personalData.filter(r=> r[0].toString() === management.persid && r[1] === management.lname)

  const wsInventar = ss.getSheetByName("Inventar")
  const inventarName = wsInventar.getRange(2,1,wsInventar.getLastRow() -1,1).getValues()

  const matchingInventar = inventarName.filter (r=> r[0] === management.gerid)

  if (matchingPersonal.length !== 1){
    throw new Error ("Check IN or Check OUT Failed")
  if (matchingInventar.length !== 1){
    throw new Error ("Check IN or Check OUT Failed")

  const idsData = wsData.getRange(2,4,wsData.getLastRow() -1,2).getValues()
  const matchingIdsData = idsData.filter(r => r[0].toString() === management.gerid)


  const latestAction = matchingIdsData.length === 0 ? "Check IN" : matchingIdsData[matchingIdsData.length -1][1]


   if(latestAction === management.action){
    throw new Error ("Check IN or Check OUT Failed")

   wsData.appendRow([new Date(),management.lname,management.persid,management.gerid,management.action])

Extra: I only have a general error message for when something is not correctly inserted, but I would like them to be more concise depending on the action which is wrong (last name not valid, personal code not valid etc.). This is however not so urgent. I appreciate any help. :pray:

You can try handling your errors:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.