问题描述
此问题是以下问题的后续问题:可以可以使用GAS创建一个交互式Web应用程序吗?
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:
- 向用户询问其用户号码
- 向用户询问电子表格中的行号(如果他们知道的话)以及一些其他信息.
- 如果他们不知道该行,请为他们获取并显示该行.
这显然需要一个if/else语句.我只是不知道如何形成它.
This obviously requires an if/else statement. I just don't know how to form it.
我试图将代码放在客户端和服务器端,但均无效.我还意识到,有些JS似乎在客户端运行,而有些则没有.因此,我总体上对客户端JS进行了研究,但是没有找到关于哪些行得通和行不通的具体规则.
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.
<!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]开始执行
- [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秒]
推荐答案
修订/更正的html
完全解决了这个问题.修改后的html在下面.难题的最后一步是在未输入songId时使其等于0,然后在gs侧测试零.因为,如果songId字段留为空白,则将其呈现为"undefined".这打破了gs方面的if/else语句.
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>
这篇关于可以在Google Script WebApp的HTML端使用If/Else语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!