gpt4 book ai didi

javascript - 运行 Google Web 应用程序脚本后出现空白屏幕

转载 作者:行者123 更新时间:2023-12-03 05:35:59 26 4
gpt4 key购买 nike

我正在通过 Google 表格开发一个签到应用程序,并希望创建一个搜索功能,该功能将运动名称作为 HTML 表单中的输入,然后从 HTML 表格中的工作表返回有关运动的信息。但是,当我尝试测试网络应用程序时,没有任何反应。我该如何解决这个问题?

这是我的代码:

Index.html

<!DOCTYPE html>
<html>
<head>
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
</head>
<body>

<fieldset id="tasks-panel">
<legend>Sports</legend>

<form name="sport-form" id="sport-form">
<label for="sport-name">Search a sport by name:</label>
<input type="text" name="sport-name" id="sport-name" />
<button onclick='addTable()' id='submit-button'>Press this</button>
</form>

<p>List of things:</p>
<div id="toggle" style="display:none"></div>
</fieldset>

<?!= HtmlService.createHtmlOutputFromFile('Javascript').getContent(); ?>

</body>
</html>

Javascript.html

<script> 
function addTable() {
var sportInput = $('sport-name').value();
var columnNames = ["Names", "Times"];
var dataArray = google.script.run.getSportData(sportInput);


var myTable = document.createElement('table');
$('#divResults').append(myTable);

var y = document.createElement('tr');
myTable.appendChild(y);

for(var i = 0; i < columnNames.length; i++) {
var th = document.createElement('th'),
columns = document.createTextNode(columnNames[i]);
th.appendChild(columns);
y.appendChild(th);
}

for(var i = 0 ; i < dataArray.length ; i++) {
var row= dataArray[i];
var y2 = document.createElement('tr');
for(var j = 0 ; j < row.length ; j++) {
myTable.appendChild(y2);
var th2 = document.createElement('td');
var date2 = document.createTextNode(row[j]);
th2.appendChild(date2);
y2.appendChild(th2);
}
}
}
</script>

代码.gs

//Setting up global variables
var ss = SpreadsheetApp.openById("-spreadsheetID-");
var sheet = ss.getSheetByName("Sheet1");

var sportsFromSheet = sheet.getRange("D4:D12");
var namesFromSheet = sheet.getRange("B4:B12").getValues();
var timesFromSheet = sheet.getRange("A4:A12").getValues();
var NAMES = [];
var TIMES = [];
var OUTPUT = [];

//doGet function
function doGet() {
return HtmlService.createTemplateFromFile('Index').evaluate()
.setTitle('Check In Data')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

//Gets both names and times of checked-in people
function getSportData(input) {
var sportInput = input;
getNamesInSport(sportInput);
getTimesInSport(sportInput);

OUTPUT = [
[NAMES],
[TIMES]
];

Logger.log(OUTPUT);
return OUTPUT;
}

//Puts the names of every person from an inputted sport into an array.
function getNamesInSport(input) {
var data = sportsFromSheet.getValues();

for (var i = 0; i < data.length; i++) {
if(data[i] == input){
NAMES.push(namesFromSheet[i][0]);
}
}
}

//Puts the times of every person from an inputted sport into an array.
function getTimesInSport(input){
var data = sportsFromSheet.getValues();

for (var i = 0; i < data.length; i ++) {
if(data[i] == input){
TIMES.push(timesFromSheet[i][0]);
}
}
}

最佳答案

JQuery id 选择器必须以 # 为前缀,以便从“运动名称”输入中获取值,您需要使用它进行选择

var sportInput = $('#sport-name').val();

此外,正如 Robin 上面评论的那样,如果您想使用 JQuery 库,您需要加载它,您显示的代码表明您可能没有这样做?

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

尽管如果是这种情况,您可能会立即看到“$ 未定义”错误。

关于javascript - 运行 Google Web 应用程序脚本后出现空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com