jQuery Check if button clicked True

Viewed 40

I have a container bound script which displays an HTML sidebar where the user enters an item ID to delete the row in which the ID is located.

Before the user deletes the item, I want to display a custom alert where the user has to confirm the deletion by pressing the confrimDelete button in the pop-up.

The Problem:
I need the confrimDelete button to run:
1

google.script.run.deleteItems(i);
$('#removeElectronicsFrom').trigger("reset");

And the cancelDelete button needs to run:
2

$('#alert').fadeOut(200)
$('#removeElectronicsFrom').trigger("reset");

I thought i could achieve this by using .data() but as you can see my current code dosen't work what so ever, i tried using the .click() method but this also dosen't work because google.script.run.deleteItems(i); has to be run inside the Form event Listener, i don't know why but it won't run outside it.

Flow
enter ID & submit ⇨ $('#alertwrapper').show(); ⇨ if confirmDelete == true run 1 ⇨ else if cancelDelete == true run 2.

GS

function getItemName(formObject) {
  const electronicsID = formObject.electronicsLocalID;
  const SHEET = getSheet();
  const RANGE = SHEET.getDataRange();
  const DELETE_VAL = electronicsID;
  const ITEMNAMECOL = 2;
  const LOCAL_ID = 1;
  const rangeVals = RANGE.getValues();
  for (var i = rangeVals.length - 1; i >= 0; i--) {
    if (rangeVals[i][LOCAL_ID] === DELETE_VAL) {
      return {
        i,
        itemName: rangeVals[i][ITEMNAMECOL],
      };
    }
  }
  return {i, itemName: "not found"};
}
function getSheet() {
  const SS = SpreadsheetApp.openById(
    '1GSzlzj7nHPIUt-RIJfsPFobtnLbuoXedtJk1x11BdT0'
  );
  const SHEET = SS.getSheetByName('ElektronikBestand');
  return SHEET;
}
function deleteItems(i) {
  getSheet().deleteRow(i + 1);
}

HTML


<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include('Stylesheet'); ?>
    <?!= include('jQuery'); ?>
    <?!= include('lottieplayer'); ?>
  </head>
  <body>
  
    <div id="loading">
        <lottie-player id="mainlottie-load" src="https://static.staticsave.com/lottie/spinner.json"  
            background="transparent"  
            speed="1" 
            loop 
            autoplay>
            </lottie-player>
        <p class="loadingtext">Loading<span>.</span><span>.</span><span>.</span></p>
      </div>
    
      <div class="sidebarwrapper">
    
          <div class="xbuttonwrapper">
              <button class="xbutton" onclick="google.script.host.close()">
              <svg class="x" enable-background="new 0 0 212.982 212.982" viewBox="0 0 212.98 212.98" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path d="m131.8 106.49 75.936-75.936c6.99-6.99 6.99-18.323 0-25.312-6.99-6.99-18.322-6.99-25.312 0l-75.937 75.937-75.937-75.938c-6.99-6.99-18.322-6.99-25.312 0-6.989 6.99-6.989 18.323 0 25.312l75.937 75.936-75.937 75.937c-6.989 6.99-6.989 18.323 0 25.312 6.99 6.99 18.322 6.99 25.312 0l75.937-75.937 75.937 75.937c6.989 6.99 18.322 6.99 25.312 0s6.99-18.322 0-25.312l-75.936-75.936z" clip-rule="evenodd" fill-rule="evenodd"/></svg>
              </button>
          </div>
    
          <div class="titlewrapper">
              <img class="ctlogotitle" src="https://i.imgur.com/d1VMjvs.png">
              <h1 class="title">Elektronik <br> Entfernen</h1>
          </div>
    
          <div class="divider"></div>
    
          <form class="inputformwrapper" id="removeElectronicsFrom">
              
              <div class="inputblockwrapper">
                  <div class="labelwrapper">
                      <label class="requiredlabel" for="electronicsLocalID">Lokale ID</label>
                  </div>
    
                  <input class="inputfield" 
                      type="text"
                      placeholder="PREF00000001..."
                      minlength="12"
                      maxlength="12"                
                      id="electronicsLocalID"
                      name="electronicsLocalID"                    
                      required>
              </div>
    
              <div class="confirmbuttonwrapper">
                  <input class="confirmbutton" 
                      type="submit" 
                      value="Delete"                
                      id="removeElectronics">
              </div>
    
          </form>
    
      </div>
      <div class="alertwrapper" id="alert">
          <div class="alertbox">
              <div class="xbuttonwrapper">
                  <button class="xbutton" id="cancleDelete">
                      <svg class="x" enable-background="new 0 0 212.982 212.982" viewBox="0 0 212.98 212.98" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path d="m131.8 106.49 75.936-75.936c6.99-6.99 6.99-18.323 0-25.312-6.99-6.99-18.322-6.99-25.312 0l-75.937 75.937-75.937-75.938c-6.99-6.99-18.322-6.99-25.312 0-6.989 6.99-6.989 18.323 0 25.312l75.937 75.936-75.937 75.937c-6.989 6.99-6.989 18.323 0 25.312 6.99 6.99 18.322 6.99 25.312 0l75.937-75.937 75.937 75.937c6.989 6.99 18.322 6.99 25.312 0s6.99-18.322 0-25.312l-75.936-75.936z" clip-rule="evenodd" fill-rule="evenodd"/></svg>
                  </button>
              </div>
              <div class="alerttitlewrapper">
                  <h1 class="alerttitle deleteitem"> Are you sure that you want to delete<span id="itemSpan"></span>?</h1>
              </div>
              <div class="alert confirmbuttonwrapper" style="order: 3;">
                  <input type="button" class="alert confirmbutton" value="Ja" id="confrimDelete">
                  <input type="button" class="alert confirmbutton" value="Nein" id="cancleDelete" >
              </div>
          </div>
      </div>

  <script>
      $( document ).ready(function() {
          $('#alert').hide()

          $("#confrimDelete").click(function() {
              $(this).data('clicked', true);
          });
          $("#cancleDelete").click(function() {
              $(this).data('clicked', true);
          });
      });

      document.querySelector("#removeElectronicsFrom").addEventListener("submit", function (e) {
            e.preventDefault();
            $('#alert').fadeIn(200);
            google.script.run.withSuccessHandler(({i, itemName}) => {
              $('#itemSpan').html(itemName);
              if($("#confrimDelete").data('clicked')) {
                  google.script.run.deleteItems(i);
                  $('#removeElectronicsFrom').trigger("reset");
              } else if($("#cancleDelete").data('clicked')) {
                  $('#alert').fadeOut(200)
                  $('#removeElectronicsFrom').trigger("reset");
              }
            }).getItemName(this);
          });
      </script>
  </body>
</html>
Column B Column C
ID itemName
-- --
MUCH00000001 Item1
MUCH00000002 Item2
MUCH00000003 Item3
MUCH00000004 Item4
MUCH00000005 Item5
MUCH00000006 Item6
MUCH00000007 Item7
MUCH00000008 Item8
MUCH00000009 Item9
MUCH00000010 Item10
1 Answers

I would consider doing it this way:

function getItemName(obj) {
  const id = obj.ID;
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const rg = sh.getRange(2,1,sh.getLastRow() - 1,sh.getLastColumn());
  const vs = rg.getValues();
  vs.forEach((r,i)  => {}) {
    if(r[1] == id ) {
      let r = SpreadsheetApp.getUi().prompt("Confirm Delete",`Are you sure you wish to delete ${r[2]}`,SpreadsheetApp.getUi().ButtonSet.YES_NO);
      if(r.getSelectedButton() == SpreadsheetApp.getUi().Button.YES) {
        sh.deleteRow(i+2);
        return {del: true}
      } else {
        return {del: false}
      }
    }
  }
}

And simplifying your html as required

Related