我在 Google Apps Script 平臺上制作了一個網路應用程式。它在測驗部署中完美加載,但一旦部署,它只會加載第一個標題
<h1>Pre-Application Test</h1> 和最后一個按鈕。<br><button onclick="submitAnswer()">Submit Answer</button>我做錯了什么?
這是代碼。QNA 是我在 doGet 函式中傳遞的值的字典
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Pre-Application Test</h1>
<h4> <?!= QNA["Question"] ?> </h4>
<form>
<input type="radio" name="choice" value= <?!= QNA["A"] ?> > <?!= QNA["A"] ?>
<br><input type="radio" name="choice" value= <?= QNA["B"] ?> > <?= QNA["B"] ?>
<br><input type="radio" name="choice" value= <?= QNA["C"] ?> > <?= QNA["C"] ?>
<br><input type="radio" name="choice" value= <?= QNA["D"] ?> > <?= QNA["D"] ?>
</form>
<br><button onclick="submitAnswer()">Submit Answer</button>
<script>
var submitAnswer = function() {
var radios = document.getElementsByName('choice');
var val= "";
for (var i = 0, length = radios.length; i < length; i ) {
if (radios[i].checked) {
val = radios[i].value;
break;
}
}
if (val == "" ) {
alert('Please choose an answer');
//break;
} else {
alert('Thankyou. Now the next question will load')
window.open('<?=ScriptApp.getService().getUrl();?>?module=Grammar',name="_top")
}
};
</script>
</body>
</html>
這是 GoogleScripts 代碼(不包括檔案 ID):
//Global Variables
var PATSpreadsheetId = ID GOES HERE
var ResultsTabName = TAB NAME GOES HERE
function FindColumnOfCorrectAnswerTo(ThisTabsName, QuestionOnRow) {
for (let col = 2; col < 6; col ) { //1 indexxed
if(SpreadsheetApp.openById(ID GOES HERE).getSheetByName(ThisTabsName).getRange(QuestionOnRow, col).getBackground() !='#ffffff') {return col} //#ffffff is the hex code for no backfill colour
}
return 'No highlit answer found'
}
//why have this? So the spreadsheet details aren't viewable on the source code for the page
function Append(ThisResultSet,ToThisTab=ResultsTabName,
OfThisSpreadsheetId=PATSpreadsheetId){
SpreadsheetApp.openById(OfThisSpreadsheetId).getSheetByName(ToThisTab).appendRow(ThisResultSet)
;
}
function GenerateDropdownOptions(FromThisSpreadsheetId = PATSpreadsheetId, InThisTabName='List of course options',InColumnNumber=1,BeginningInRowNumber=2){
Tabitha = SpreadsheetApp.openById(FromThisSpreadsheetId).getSheetByName(InThisTabName)
cells = Tabitha.getSheetValues(BeginningInRowNumber,InColumnNumber,Tabitha.getLastRow(),1)
options=[]
for (let rower = 1; rower < cells.length ; rower ) { //1 indexxed
options.push('<option>' cells[rower] '</option>')
}
return options.join('')
}
function GetQuestionAndAnswers(FromThisSpreadsheetId=PATSpreadsheetId,InThisTabName){
Tabitha = SpreadsheetApp.openById(FromThisSpreadsheetId).getSheetByName(InThisTabName)
Rower = GetRandomIntInclusive(2,Tabitha.getLastRow())
RawData = Tabitha.getSheetValues(Rower,1,1,5)[0]
return {
Question: RawData[0]
,A: RawData[1]
,B: RawData[2]
,C: RawData[3]
,D: RawData[4]
,Answer: RawData[FindColumnOfCorrectAnswerTo(InThisTabName,Rower)-1]//minus one because sporeadsheet 1 indexxed and array 0 indexxed.
}
}
function GetRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min 1) min); //The maximum is inclusive and the minimum is inclusive
}
//This function is a special function that loads the html to the webb app
function doGet(e){
//this determines which module to load
//Obviously this would be best with a switch statement, but it just wouldn't recognise the parameter. Guessing it was some sort of object thing. Anyway we'll just have to if else till the cows come home
if(e.parameters.module=='Grammar'){
var Siter = HtmlService.createTemplateFromFile('Grammar Test')
//now we pass variables to this template
Siter.QNA = GetQuestionAndAnswers()
return Siter.evaluate();
}else{
var Siter = HtmlService.createTemplateFromFile('PAT Webb App')
//now we pass variables to this template
Siter.CourseOptions = GenerateDropdownOptions(PATSpreadsheetId,'List of course options',1,1)
return Siter.evaluate();}
}
謝謝
uj5u.com熱心網友回復:
謝謝你們的幫助
原來問題是ScriptApp.getService().getUrl()。當我更新部署時,一個視圖得到了更新,但語法視圖不知何故卡在了舊版本上!我多次嘗試重新部署,甚至歸檔所有舊視圖,但這只是給出了無法加載檔案的錯誤訊息。
解決方案是將每個視圖的完整 URL 放入每個參考它的地方。所以不再有新的部署,從現在開始只有新的版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394368.html
上一篇:For回圈不迭代變數
