Jagdish Mehra
Jagdish Mehra

Reputation: 1

Want to convert my google script web app into custom menu pop up

I made a google sheet script web app but I want to create a custom menu from where I can get my web app as pop up on the same page so that I don't need to always go to web app link or if any alternative you know please help me...

I want to make a searching operation which we can use inside our sheet or by making something in a new sheet also where we can get all the data inside the sheet.

this is a scripting file

Code. gs

function doGet() {
 
  return HtmlService.createTemplateFromFile('Index').evaluate();
}

function processForm(formObject){  
  var result = "";
  if(formObject.searchtext){
      result = search(formObject.searchtext);
  }
  return result;
}



function search(searchtext){
  var spreadsheetId   = '1JZanz4UZObZeLfN-z1RM77VcS_13pEBVcxf4pLzZ7wY'; 
  var dataRage        = 'APAC!A3:P';
                                     
  var data = Sheets.Spreadsheets.Values.get(spreadsheetId, dataRage).values;
  var ar = [];
  
  data.forEach(f=> {
    if (~f.indexOf(searchtext)) {
      ar.push(f);
    }
  });
  return ar;
}

This is an HTML file

Index.html

<!DOCTYPE html>
<html>
    <head>
        <base target="_top">
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o" crossorigin="anonymous"></script>
        
        <!--##JAVASCRIPT FUNCTIONS ---------------------------------------------------- -->
        <script>
          //PREVENT FORMS FROM SUBMITTING / PREVENT DEFAULT BEHAVIOUR
          function preventFormSubmit() {
            var forms = document.querySelectorAll('form');
            for (var i = 0; i < forms.length; i++) {
              forms[i].addEventListener('submit', function(event) {
              event.preventDefault();
              });
            }
          }
          window.addEventListener("load", preventFormSubmit, true); 
             
          
          //HANDLE FORM SUBMISSION
          function handleFormSubmit(formObject) {
            google.script.run.withSuccessHandler(createTable).processForm(formObject);
            document.getElementById("search-form").reset();
          }
        
          //CREATE THE DATA TABLE
          function createTable(dataArray) {
            if(dataArray && dataArray !== undefined && dataArray.length != 0){
              var result = "<table class='table table-sm table-striped' id='dtable' style='font-size:0.8em'>"+
                           "<thead style='white-space: nowrap'>"+
                             "<tr>"+                              
                        
                              "<th scope='col'>Manage status</th>"+
                              "<th scope='col'>Site Code</th>"+
                              "<th scope='col'>BU</th>"+
                              "<th scope='col'>Site name</th>"+
                              "<th scope='col'>SSH / Telnet</th>"+
                              "<th scope='col'>Comment</th>"+
                              "<th scope='col'>Hostname</th>"+
                              "<th scope='col'>Model</th>"+
                              "<th scope='col'>Function</th>"+
                              "<th scope='col'>AAA config</th>"+
                              "<th scope='col'>Mgt IP</th>"+
                              "<th scope='col'>Subnet</th>"+
                              "<th scope='col'>VLAN</th>"+
                              "<th scope='col'>Location</th>"+
                              "<th scope='col'>Serial No </th>"+
                              "<th scope='col'>Software Ver </th>"+
                            "</tr>"+
                          "</thead>";
              for(var i=0; i<dataArray.length; i++) {
                  result += "<tr>";
                  for(var j=0; j<dataArray[i].length; j++){
                      result += "<td>"+dataArray[i][j]+"</td>";
                  }
                  result += "</tr>";
              }
              result += "</table>";
              var div = document.getElementById('search-results');
              div.innerHTML = result;
            }else{
              var div = document.getElementById('search-results');
              //div.empty()
              div.innerHTML = "Data not found!";
            }
          }
        </script>
        <!--##JAVASCRIPT FUNCTIONS ~ END ---------------------------------------------------- -->
        
    </head>

    <style>
      th,td{
        border-left:1px dashed gray;
      }
      </style>
    <body>
        <div class="container">
            <br>
            <div class="row">
              <div class="col">
            
                  <!-- ## SEARCH FORM ------------------------------------------------ -->
                  <form id="search-form" class="form-inline" onsubmit="handleFormSubmit(this)">
                    <div class="form-group mb-2">
                      <label for="searchtext">Search Text</label>
                    </div>
                    <div class="form-group mx-sm-3 mb-2">
                      <input type="text" class="form-control" id="searchtext" name="searchtext" placeholder="Search Text">
                    </div>
                    <button type="submit" class="btn btn-primary mb-2">Search</button>
                  </form>
                  <!-- ## SEARCH FORM ~ END ------------------------------------------- -->
              
              </div>    
            </div>
            <div class="row">
              <div class="col">
            
                <!-- ## TABLE OF SEARCH RESULTS ------------------------------------------------ -->
                <div id="search-results" class="table-responsive">
                  <!-- The Data Table is inserted here by JavaScript -->
                </div>
                <!-- ## TABLE OF SEARCH RESULTS ~ END ------------------------------------------------ -->
                  
              </div>
            </div>
        </div>
    </body>
</html>

Upvotes: 0

Views: 438

Answers (1)

Cooper
Cooper

Reputation: 64100

Try:

function launchwebapdialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createTemplateFromFile('Index').evaluate(),'Title');
}

I got this to work. No it's not the same. But you can take it to the next step from here.

gs:

function processForm(formObject){  
  Logger.log(JSON.stringify(formObject));
  var result = "";
  if(formObject.searchtext){
      result = search(formObject.searchtext);
  }
  return result;
}

function search(searchtext){
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  
  const tf = sh.createTextFinder(searchtext).findAll();
  let found=tf.map(e => e.getA1Notation());
  return found;
}

function launchwebapdialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createTemplateFromFile('ah1').evaluate(),'Title');
}

html:

<!DOCTYPE html>
<html>
    <head>
        <base target="_top">
        <script>
          function handleFormSubmit(formObject) {
            google.script.run
            .withSuccessHandler(createTable)
            .processForm(formObject);
          }

          function createTable(dataArray) {
            document.getElementById("search-form").reset();
              document.getElementById('search-results').innerHTML = dataArray.join(', ');
          }        
          
        </script>
    </head>
    <body>
        <div class="container">
            <br>
            <div class="row">
              <div class="col">
                  <form id="search-form" class="form-inline">
                    <div class="form-group mb-2">
                      <label for="searchtext">Search Text</label>
                    </div>
                    <div class="form-group mx-sm-3 mb-2">
                      <input type="text" class="form-control" id="searchtext" name="searchtext" placeholder="Search Text">
                    </div>
                    <input type="button" value="Search" onclick="handleFormSubmit(this.parentNode);" />
                  </form>
                  
              </div>    
            </div>
            <div class="row">
              <div class="col">
                <div id="search-results" class="table-responsive">
                </div>                  
              </div>
            </div>
        </div>
    </body>
</html>

Upvotes: 1

Related Questions