Search code examples
javascriptgoogle-apps-scriptfrontendsweetalert2

Sweetalert Loading Screen Before .withsuccesshandler


i'm integrating sweetalert2 in my Google Workspace Addon. The code is simple.

Functions for sucess and error.

<script>
function swalsuccess() {

Swal.fire({
  title: '¡Hecho!',
  text: 'El script se ha ejecutado 👍',
  icon: 'success',
  confirmButtonText: 'Continuar'
})

}
</script>

<script>
function swalerror() {

Swal.fire({
  title: 'Error!',
  text: 'El script no ha podido ejecutarse.',
  icon: 'error',
  confirmButtonText: 'Entendido'
})

}
</script>

And a button that executes a function onclick, with the "withsuccesshandler" and "withfailurehandler" to show if the script has been applied to the Google Sheet document.

<button class="button-68" role="button" onclick="morphf354()">Eliminar filas (Max.k)</button>

<script>
function morphf354() {
  google.script.run
  .withSuccessHandler(swalsuccess)
  .withFailureHandler(swalerror)
  .deleteRows();
}
</script>

Well, the script takes few seconds to realise the function, so i want to add a loading swal screen, that changes to swalsuccess when it's done. I don't know how to implement it and I don't know how to fit my code with the solutions that i read on the internet.

Thanks


Solution

    • Fire a loader dialog before making the google.script.run
    • Close it in the success handler
    function swalsuccess() {
        Swal.close()
        Swal.fire({
          title: '¡Hecho!',
          text: 'El script se ha ejecutado 👍',
          icon: 'success',
          confirmButtonText: 'Continuar'
        })
    }
    function morphf354() {
        Swal.fire({
          titleText: "Checking data...",
          allowOutsideClick: false,
          preConfirm: Swal.showLoading,
          showLoaderOnConfirm: true,
        })
        google.script.run
          .withSuccessHandler(swalsuccess)
          .withFailureHandler(swalerror)
          .deleteRows();
        }