0

Working on an HTML integration with GAS, the following code is not doing anything but generating a white, seemingly empty form upon clicking "Add":

//GLOBALS
let ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = ss.getLastRow();
var lastCol = ss.getLastColumn();
var ui = SpreadsheetApp.getUi();
var arrEndRow;
var icaoFindMatch = 0;

//Upon opening spreadsheet, menu items are added and a test is made to determine
//if there are any entries and if not, it will load an entry form
function onOpen(e) {
    
    //creates custom menu
    ui.createMenu('Catering Menu')
        .addItem('Search Locations', 'menuItem1')
        .addItem('Add Catering Info', 'menuItem2')
        .addToUi();
  
    //test if there is at least one entry
    if (lastRow == 1){
        loadForm();  //run entry form
    } else {
        SpreadsheetApp.getActiveSpreadsheet().toast("Use the 'Catering Menu' dropdown on the toolbar to search, add or modify existing catering info.","User Notice",-1);
    }
}

function testMe(){
    ui.alert("in TestMe");
}
//takes info entered on the uForm.html and enters it under the appropriate ICAO location
function addNewRow(rowData){
    ui.alert("in addNewRow");
    /*
    var icaoResult = ui.prompt("Please input your ICAO");
    var resultText = icaoResult.getResponseText();
    */
    
    let icaoRangeInit = ss.getRange(2,1,lastRow-1,1).getValues();
    let icaoArr = icaoRangeInit.filter((string) => string != "");
    let icaoArrLast = icaoArr[icaoArr.length-1];
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title>Catering Entry Form</title>
  </head>
  <body>
    <form>
        <div class="form-floating mb-4">
            <input type="text" class="form-control" id="floatingLocation" placeholder="ICAO Location">
            <label for="floatingLocation">ICAO</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingName" placeholder="Name">
            <label for="floatingName">Name</label>
        </div>
        <div class="form-floating">
            <input type="number" class="form-control" id="floatingDistance" placeholder="Distance (mi)">
            <label for="floatingDistance">Distance (mi)</label>
        </div>
        <div class="form-floating">
            <input type="number" class="form-control" id="floatingDriveTime" placeholder="Drive Time (min)">
            <label for="floatingDriveTime">Drive Time (min)</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingPhoneNum" placeholder="Phone #">
            <label for="floatingPhoneNum">Phone Number</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingSite" placeholder="Website">
            <label for="floatingSite">Website</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingDelivery" placeholder="Delivery?">
            <label for="floatingDelivery">Delivery (Yes/No)</label>
        </div>
        <div class="form-floating">
            <textarea class="form-control" placeholder="Notes" id="floatingTextarea"></textarea>
            <label for="floatingTextarea">Notes</label>
        </div>
        <div>
            <button class="btn btn-primary" id="AddtoWS">Add</button>
        </div>
    </form>
        
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

        <!-- script to take the values inputted in fields above and transfer to spreadsheet -->
        <script>

            function afterButtonClicked() {
                google.script.run.testMe();
                var icao = document.getElementById("floatingLocation");
                var name = document.getElementById("floatingName");
                var dist = document.getElementById("floatingDistance");
                var driveTime = document.getElementById("floatingDriveTime");
                var phoneNum = document.getElementById("floatingPhoneNum");
                var site = document.getElementById("floatingSite");
                var delivery = document.getElementById("floatingDelivery");
                var notes = document.getElementById("floatingTextarea");

                var rowData = {icao: icao.value, name: name.value, dist: dist.value, driveTime: driveTime.value, phoneNum: phoneNum.value, site: site.value, delivery: delivery.value, notes: notes.value};
                //google.script.run.testMe();

            }

            document.getElementById("AddtoWS").addEventListener("click", afterButtonClicked);   //tells the "Add" button what to do

        </script>
  </body>
</html>

That's the entirety of the HTML which does everything from loading the sidebar and input fields, all the way to the button but it's not properly calling my Javascript in the Code.js

Also, I only pasted the top snippet of my Code.js because it's clear I'm not evening getting to where I can get the testMe() function called to check that the handshake between html and js is even working.

I've spent more hours trying to figure this out than I care to admit, any help would be greatly appreciated!

  • Colin

3 Answers 3

1

You have not called on onclick. You can call the function as shown below.

<div>
<button class="btn btn-primary" onclick="afterButtonClicked()" id="AddtoWS">Add</button>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to add an attribute to the button called onclick, which tells the webpage what to do if that button is clicked, for ex.

<div>
            <button class="btn btn-primary" id="AddtoWS">Add</button>
</div>

should be changed to

<div>
            <button class="btn btn-primary" id="AddtoWS" onclick="functionYouWantToRun()">Add</button>

</div>

if JS file is connected to it even with the src attribute, it will work the same, just add the code to run, or you can add an eventListener. Ex:

document.getElementById(AddtoWs).addEventListener('click',function(){
//Add the JS code you want to run
});

, it will basically run the code inside the function(){}, once someone clicks the button. Edit: I just double-checked your code and you added the eventListener, but there is a problem, you need to add the function before you use the addEventLsitener.

Edit No.2: Try adding the function in the eventListener, I made a mistake in the first edit. Edit No,3: Try this code, This should work:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <!-- Refering the JS code you had first put-->
    <script src="anyname.js"></script>
    <!---->

    <title>Catering Entry Form</title>
  </head>
  <body>
    <form>
        <div class="form-floating mb-4">
            <input type="text" class="form-control" id="floatingLocation" placeholder="ICAO Location">
            <label for="floatingLocation">ICAO</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingName" placeholder="Name">
            <label for="floatingName">Name</label>
        </div>
        <div class="form-floating">
            <input type="number" class="form-control" id="floatingDistance" placeholder="Distance (mi)">
            <label for="floatingDistance">Distance (mi)</label>
        </div>
        <div class="form-floating">
            <input type="number" class="form-control" id="floatingDriveTime" placeholder="Drive Time (min)">
            <label for="floatingDriveTime">Drive Time (min)</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingPhoneNum" placeholder="Phone #">
            <label for="floatingPhoneNum">Phone Number</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingSite" placeholder="Website">
            <label for="floatingSite">Website</label>
        </div>
        <div class="form-floating">
            <input type="text" class="form-control" id="floatingDelivery" placeholder="Delivery?">
            <label for="floatingDelivery">Delivery (Yes/No)</label>
        </div>
        <div class="form-floating">
            <textarea class="form-control" placeholder="Notes" id="floatingTextarea"></textarea>
            <label for="floatingTextarea">Notes</label>
        </div>
        <div>
            <button class="btn btn-primary" id="AddtoWS">Add</button>
        </div>
    </form>
        
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

        <!-- script to take the values inputted in fields above and transfer to spreadsheet -->
        <script>

            /*function afterButtonClicked() {
                

            }*/

            document.getElementById("AddtoWS").addEventListener('click', function(){
                google.script.run.testMe();
                var icao = document.getElementById("floatingLocation");
                var name = document.getElementById("floatingName");
                var dist = document.getElementById("floatingDistance");
                var driveTime = document.getElementById("floatingDriveTime");
                var phoneNum = document.getElementById("floatingPhoneNum");
                var site = document.getElementById("floatingSite");
                var delivery = document.getElementById("floatingDelivery");
                var notes = document.getElementById("floatingTextarea");

                var rowData = {icao: icao.value, name: name.value, dist: dist.value, driveTime: driveTime.value, phoneNum: phoneNum.value, site: site.value, delivery: delivery.value, notes: notes.value};
                //google.script.run.testMe();
            });   //tells the "Add" button what to do

        </script>
  </body>
</html>

,anyname.js:

//GLOBALS
let ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = ss.getLastRow();
var lastCol = ss.getLastColumn();
var ui = SpreadsheetApp.getUi();
var arrEndRow;
var icaoFindMatch = 0;

//Upon opening spreadsheet, menu items are added and a test is made to determine
//if there are any entries and if not, it will load an entry form
function onOpen(e) {
    
    //creates custom menu
    ui.createMenu('Catering Menu')
        .addItem('Search Locations', 'menuItem1')
        .addItem('Add Catering Info', 'menuItem2')
        .addToUi();
  
    //test if there is at least one entry
    if (lastRow == 1){
        loadForm();  //run entry form
    } else {
        SpreadsheetApp.getActiveSpreadsheet().toast("Use the 'Catering Menu' dropdown on the toolbar to search, add or modify existing catering info.","User Notice",-1);
    }
}

function testMe(){
    ui.alert("in TestMe");
}
//takes info entered on the uForm.html and enters it under the appropriate ICAO location
function addNewRow(rowData){
    ui.alert("in addNewRow");
    /*
    var icaoResult = ui.prompt("Please input your ICAO");
    var resultText = icaoResult.getResponseText();
    */
    
    let icaoRangeInit = ss.getRange(2,1,lastRow-1,1).getValues();
    let icaoArr = icaoRangeInit.filter((string) => string != "");
    let icaoArrLast = icaoArr[icaoArr.length-1];

7 Comments

Thank you all for your help. Sadly, none of the suggest above are working. I'm still getting a white screen in the sidebar once "Add" has been clicked, please reference: Before button is clicked: drive.google.com/file/d/1HnQsI8kM3TteKCgMh4EGTFgWKcXfhwde/… After button is clicked: drive.google.com/file/d/1cnfgUJ8nH1hz5aNB7Sxz_mNStLSS5U4O/…
@ColinCowger The images are deleted, is there a confirmation message that will appear once you click on that button, like button.addEventListener('click',function(){..............; document.getElementById('divThatYouWantToAppear').style.display= block;});
@ColinCowger, can you host the code to a temporary website, I have already made the site and account. Go to cpanel.epizy.com, add epiz_28450601 as the username, and lLB1cNGb3QzI8 as the password, then find the file manager and click on it. Click on the folder 'colincowgerhelp.ml', then 'htdocs', right-click on the blank space and click new file, type "index.html" and click ok. Type the HTML code and click save, then click cancel. Then create another new file and name it '(theNameOfYourExternalJsFile).js' and add the External JS code. I have to check the problem.
@ColinCowger, also sorry for the late reply, I was busy
@ColinCowger, you have 1 year till the free hosting time expires, so please do the steps as soon as possible
|
0
<title>[Best Catering Service in Hyderabad][1] By | Shahi Pakwaan</title>
<meta name="description" content="Best catering service provider in Hyderabad we offer exceptional catering services for various events and budgets. Book Now!" />
<link rel="canonical" href="https://shahipakwaan.in/" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Best Catering Service in Hyderabad By | Shahi Pakwaan" />
<meta property="og:description" content="Best catering service provider in Hyderabad we offer exceptional catering services for various events and budgets. Book Now!" />
<meta property="og:url" content="https://shahipakwaan.in/" />
<meta property="og:site_name" content="shahipakwaan" />
<meta property="article:modified_time" content="2023-06-24T11:42:46+00:00" />
<meta property="og:image" content="https://shahipakwaan.in/wp-content/uploads/2023/03/shahi-pakwan11-1024x626-1.png" />
<meta name="twitter:card" content="summary_large_image" />
<script type="application/ld+json" class="yoast-schema-graph">{"@context":"https://schema.org","@graph":[{"@type":"WebPage","@id":"https://shahipakwaan.in/","url":"https://shahipakwaan.in/","name":"Best Catering Service in Hyderabad By | Shahi Pakwaan","isPartOf":{"@id":"https://shahipakwaan.in/#website"},"primaryImageOfPage":{"@id":"https://shahipakwaan.in/#primaryimage"},"image":{"@id":"https://shahipakwaan.in/#primaryimage"},"thumbnailUrl":"https://shahipakwaan.in/wp-content/uploads/2023/03/shahi-pakwan11-1024x626-1.png","datePublished":"2023-01-23T19:39:39+00:00","dateModified":"2023-06-24T11:42:46+00:00","description":"Best catering service provider in Hyderabad we offer exceptional catering services for various events and budgets. Book Now!","breadcrumb":{"@id":"https://shahipakwaan.in/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://shahipakwaan.in/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://shahipakwaan.in/#primaryimage","url":"https://shahipakwaan.in/wp-content/uploads/2023/03/shahi-pakwan11-1024x626-1.png","contentUrl":"https://shahipakwaan.in/wp-content/uploads/2023/03/shahi-pakwan11-1024x626-1.png","width":1024,"height":626},{"@type":"BreadcrumbList","@id":"https://shahipakwaan.in/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home"}]},{"@type":"WebSite","@id":"https://shahipakwaan.in/#website","url":"https://shahipakwaan.in/","name":"shahipakwaan","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://shahipakwaan.in/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"}]}</script>
<!-- / Yoast SEO plugin. -->

https://shahipakwaan.in/

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.