1

This question is a follow-on question to: Can one create an Interactive web app with GAS?

I am writing a script that should do the following:

  • Ask the user for their user number
  • Ask the user for a row number from a spreadsheet (if they know it) and some additional info.
    • If they don't know the row, get the row for them and display it.
  • Ask them for the additional info.

This obviously requires an if/else statement. I just don't know how to form it.

I have attempted to put the code on the client and server side and neither works. I have also realized that some JS seems to run on the client side and some does not Accordingly, I have researched client side JS, in general, but not found specific rules for what works and doesn't.

html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <center>
Enter your User ID below. If you are re-using a row in your Catalog Spreadsheet (meaning you know the row number),<br>
enter it below in the "Song ID" field, enter the Song Title, and then click the "Continue" button. Else, enter your<br> 
User ID only and click the "Continue" button. We will find the next avalable empty row at the end of your Catalog<br>
Spreadsheet and display its value to you. Then you may enter it and your Song Title. After you do, click the<br> 
"Continue" button to create the lyric sheet and add the new song to your Catalog Spreadsheet.<br><br>

Note: We will automatically add your name to the lyric sheet as the songwriter. Add additional writers manually<br>
on the lyric sheet.<br><br>

<div>
<input id="user" type="text" placeholder="User ID"><br><br>
<div  id="results"></div>
</div>
<input id="song" type="text" placeholder="Song ID"><br><br>
<input id="title" type="text" placeholder="Song Title"><br><br>
<input type="button" value="Continue" onclick="saveUserInput()"><br><br>
</center>
<script>
      
   function saveUserInput() {
     var userId = document.getElementById('userId').value;
     var songId = document.getElementById('userId').value;
      if(songId != ""){
        window.saveUserInput = function() {
         var userId = document.getElementById('userId').value;
         var songId = document.getElementById('songId').value;
         var songTitle = document.getElementById('idNewSongTitle').value;
             console.log('songTitle: ' + songTitle)
         google.script.run
             .withSuccessHandler(openPrompt)
             .getSongId({userId:userId, songId:songId, songTitle:songTitle})
      }
      }
      else {
       google.script.run
          .withSuccessHandler(function(hl){
            document.getElementById('results').innerHTML=hl;
          })
          .getSongId({userId:userId})
      }

       function openPrompt(results){
           window.open(results.url, '_blank').focus();
      }
      }
    </script>
  </body>
</html>

gs

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

function getSongId(uObj) {
  var userId = uObj.userId;
  var songId = uObj.songId;
  var songTitle = uObj.songTitle;
    
   if(songId !=""){
        
Code not included to keep this brief, but it has been tested in other applications of this project and works and what it does is: if the user has entered a Song ID, this code creates the new lyric sheet and adds the new song name to the Catalog SS.
     
      }
     else{
        
This code does this:
     return ('The next available row for the new song is '+songId);
        }
}

When I run what I have the Execution Transcript output is:

  • [19-05-29 07:54:16:951 EDT] Starting execution
  • [19-05-29 07:54:16:959 EDT] HtmlService.createHtmlOutputFromFile([Index]) [0 seconds]
  • [19-05-29 07:54:16:961 EDT] HtmlOutput.getContent() [0 seconds]
  • [19-05-29 07:54:16:961 EDT] HtmlOutput.getTitle() [0 seconds]
  • [19-05-29 07:54:16:962 EDT] Execution succeeded [0.003 seconds total runtime]
Community
  • 1
  • 1
Bee Tee
  • 129
  • 2
  • 15
  • You need to state exactly what doesn't work. The `doGet()` function gets triggered from a GET request being made to the published url of the Web App, or the dev test link. When you enter a link in the address bar of the browser and hit Enter, a GET request is made. If you want a dialog box to appear in the spreadsheet, then don't use `doGet()`, create a function with a different name to display a dialog box. Does the user see the spreadsheet, or will they never see the spreadsheet? In the browser, open the developer tools and look in the console for any errors. The f12 key opens that. – Alan Wells May 29 '19 at 12:23
  • In addition to Alan's comment, could you please clarify, what's the point in overriding your `saveUserInput()`? Judging from the code: if `songId` is provided -> override func. Maybe you want to run it instead? – Oleg Valter is with Ukraine May 29 '19 at 13:27
  • Regardless of useful comments provided above, If the question is Can I use if/else in html side, then yes. – TheMaster May 29 '19 at 13:47
  • @AlanWells, the Web App is triggered from a button on a Google (new) Site. The user spreadsheet is NOT open. That's why the user does not necessarily know what the next available row on the spreadsheet. This necessitates the two-part process where the code asks whether the user does know the row number (which is the songId). If not, the code finds the next available row in the user's spreadsheet. What doesn't work, per the Execution Transcript which I posted above is that the gs script does not ever run. I suspect the error is on the client side, but I do not know how to look for errors there. – Bee Tee May 29 '19 at 15:54
  • @TheMaster: Thanks for that answer. It helps a lot. I suspect then that there is an error in the if/else code I wrote there (rather than, one just can't use if/else code there). Any pointers on what I messed up? – Bee Tee May 29 '19 at 15:57
  • @OlegValter: If I understand your question, I think the answer is that I don't know how to declare the songId variable so that it can be used in the if statement, and erroneously used the `saveUserInput()` line not realizing I was doubling things up. – Bee Tee May 29 '19 at 16:10
  • @BeeTee - not exactly, my question to you is most likely the answer to your problem - if you want the server-side code to execute, then you need to actually run the event listener, not to rewrite it (btw, does the code run if `songId` equals to empty string?) – Oleg Valter is with Ukraine May 29 '19 at 16:47
  • @OlegValter: No, it does not run. There are essentially 2 cases: 1: `songId` entered, 2: `songId` not entered. In both cases, the code never seems to get to the server (gs) side. – Bee Tee May 29 '19 at 18:10
  • Previous comment now na. Got it working. See revised code. However still missing the very last bit as noted. – Bee Tee May 30 '19 at 01:09

1 Answers1

0

Revised/Corrected html

Got this completely working. revised html is below. The final piece of the puzzle was to make the songId equal to 0 when it is not entered and then test for zero in the gs side. Because, if the songId field is left blank, it renders as "undefined" which was breaking the if/else statement on the gs side.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <center>
Enter your User ID below. 

<div>
<input id="user" type="text" placeholder="User ID"><br><br>
<div  id="results"></div>
</div>
<input id="song" type="text" placeholder="Song ID"><br><br>
<input id="title" type="text" placeholder="Song Title"><br><br>
<input type="button" value="Continue" onclick="saveUserInput()"><br><br>

</center>
<script>

   function saveUserInput() {
     var userId = document.getElementById('user').value;
     var songId = document.getElementById('song').value;
     var songTitle = document.getElementById('title').value;

         console.log('songTitle: ' + songTitle)

   // If songId is supplied by user...
   if(songId != ""){
      google.script.run
          .withSuccessHandler(openNewDoc)
          .getSongId({userId:userId, songId:songId, songTitle:songTitle})

     function openNewDoc(results){
           window.open(results.url, '_blank').focus();
   }
   }
    // If songId is blank
    else {
       var songId = 0;
       google.script.run
          .withSuccessHandler(function(hl){
            document.getElementById('results').innerHTML=hl;
          })
          .getSongId({userId:userId, songId:songId})
        }
 }
    </script>
  </body>
</html>
Community
  • 1
  • 1
Bee Tee
  • 129
  • 2
  • 15
  • Thanks to all for your help. Great learning experience and a critical part of the app I am trying to build. Tall hill to climb to learn this kind of coding in my dotage! – Bee Tee May 30 '19 at 22:40