Search code examples
google-apps-scriptsweetalert2

Is there a way to reference sweet alert from google apps script?


I'm linking the library with the src attribute and using a function to call it and its not working

GS:

function doGet(e) { 
  var params = JSON.stringify(e.parameters) 
  var params2 =JSON.parse(params) 
  cache.put("name", params2.name)
  cache.put("DBID", params2.DBID)
  return HtmlService.createTemplateFromFile("test").evaluate()
}

function include(f1){ 
  return HtmlService.createHtmlOutputFromFile(f1).getContent();
} 

Html:

<head>
  <title>Email form test</title>
  <?!= include("CSS") ?>
</head>
<body>   
   <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8.17.6/dist/sweetalert2.all.js"></script>
   <?!= include('Javascript') ?>
   <button type="button" name="Submit" onclick="javascript:t1();"id="sub1"class="btn btn-white btn-animation-1">Submit</button>

Calling library (after its been initialized above):

<script>
function t1(){
  Swal.fire('Any fool can use a computer');
}
</script>

the expected result should be I click the button and "any fool can use a computer" should pop up in a sweet alert 2 box


Solution

  • You don't need to import and evaluate the Sweetalert library within Apps Script - you can include it in your HTML file as you would normally and return the HTML Output from file on doGet():

    code.gs:

    function doGet(e) { 
      // your code here
      return HtmlService.createHtmlOutputFromFile("index");
    }
    

    and index.html:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Email form test</title>
      </head>
      <body>   
       <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8.17.6/dist/sweetalert2.all.js"></script>
    
       <button type="button" name="Submit" onclick="t1();"id="sub1"class="btn btn-white btn-animation-1">Submit</button>
        <script>
          function t1(){
            Swal.fire('Any fool can use a computer');
          }
        </script>
      </body>
    </html>