可以在Google Script WebApp的HTML端使用If / Else语句吗?

时间:2019-05-29 12:01:11

标签: google-apps-script

此问题是针对以下问题的后续问题:Can one create an Interactive web app with GAS?

我正在编写一个脚本,该脚本应执行以下操作:

  • 询问用户的用户号码
  • 向用户询问电子表格中的行号(如果他们知道的话)以及一些其他信息。
    • 如果他们不知道该行,请为他们获取并显示该行。
  • 向他们询问其他信息。

这显然需要一个if / else语句。我只是不知道如何形成它。

我试图将代码放在客户端和服务器端,但均无效。我还意识到,有些JS似乎在客户端运行,而有些则没有。因此,我总体上对客户端JS进行了研究,但未找到适用于无效方法的特定规则。

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);
        }
}

运行时,我的“执行成绩单”输出为:

  • [19-05-29 07:54:16:951 EDT]开始执行
  • [li] [19-05-29 07:54:16:959 EDT] HtmlService.createHtmlOutputFromFile([Index])[0秒]
  • [19-05-29 07:54:16:961 EDT] HtmlOutput.getContent()[0秒]
  • [19-05-29 07:54:16:961 EDT] HtmlOutput.getTitle()[0秒]
  • [19-05-29 07:54:16:962 EDT]执行成功[总运行时间0.003秒]

1 个答案:

答案 0 :(得分:0)

修订/更正的html

完全正常工作。修改后的html在下面。难题的最后一步是在未输入songId时使其等于0,然后在gs侧测试零。因为,如果songId字段留为空白,则会呈现为“未定义”,这会破坏gs一侧的if / else语句。

<!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>