'Google App Script: modify destination of .createTemplateFromFile()
I have a trouble with a code (in origin in https://codylab.blogspot.com/2021/08/publish-student-result-using-google.html, here a Live Demo https://script.google.com/macros/s/AKfycbyAo9bTNRJXoba0bnHlEz2y0ZD0qzTtc51vy4v4OKjt_BmUD-i6tpFSVUXoBRDY9AvD/exec and here video https://www.youtube.com/watch?v=ApfItwu_J54&list=PL2FSU6y-MoWX4ycHLVSEz-bNxTuZMXF4F&index=8).
With this code
student easly can check their result by putting their id number
I would like to edit the code so that the landing page template can change (e.g. a "result.html" page for class I, a "result2.html" page for class II, etc.). I would like to change the following code
var htmlOutput = HtmlService.createTemplateFromFile(risultato);
I have tried to insert a condition if-else (in code.gs) but I can't obtain the desired result.
if (cls.getValue = "I") {
risultato='result1'
} else {
risultato='result2'
}
...
var htmlOutput = HtmlService.createTemplateFromFile(risultato);
Can you help me?
Best regards BP
CODE:GS
function doGet(e) {
var htmlOutput = HtmlService.createTemplateFromFile('home');
htmlOutput.result = 'Home';
return htmlOutput.evaluate();
}
function doPost(e){
Logger.log(JSON.stringify(e));
if(e.parameter.Submit == 'Get Result'){
var cls = e.parameter.cl;
var roll = e.parameter.rl;
var ss = SpreadsheetApp.openById('/////////');
var ws = ss.getSheetByName(cls)
var lr = ws.getLastRow();
for(var i = 1;i <= lr;i++){
var val = ws.getRange(i, 1).getValue();
if(val == roll) {
var v1 = ws.getRange(i, 2).getValue();
var v2 = ws.getRange(i, 3).getValue();
var v3 = ws.getRange(i, 4).getValue();
var v4 = ws.getRange(i, 5).getValue();
var v5 = ws.getRange(i, 6).getValue();
var v6 = ws.getRange(i, 7).getValue();
var v7 = ws.getRange(i, 8).getValue();
var v8 = ws.getRange(i, 9).getValue();
var v9 = ws.getRange(i, 10).getValue();
}}
risultato='result'
var htmlOutput = HtmlService.createTemplateFromFile(risultato);
htmlOutput.cl = cls;
htmlOutput.id = roll;
htmlOutput.stn = v1;
htmlOutput.fa = v2;
htmlOutput.tm = v3;
htmlOutput.re = v4;
htmlOutput.hi = v5;
htmlOutput.en = v6;
htmlOutput.mt = v7;
htmlOutput.sc = v8;
htmlOutput.co = v9;
return htmlOutput.evaluate();
}else{
var htmlOutput = HtmlService.createTemplateFromFile('home');
htmlOutput.result = '';
return htmlOutput.evaluate();
}};
function getUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
HOME.HTML
<!DOCTYPE html>
<center>
<html>
<head>
<base target="_top">
<style>
select,input{
padding:5px;
width:100%;
border:1px solid;
margin:1px;
font-family:times}
input:hover{
background:orange;}
</style>
</head>
<body>
<table border="0" width='200px' cellspacing="0" >
<tr>
<td width=20% align=center>
<?var url = getUrl();?>
<form method="post" action="<?= url ?>" >
<select name='cl'>
<option value="0">Select Class:</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</br></br>
<input type="text" name="rl" placeholder="Roll No">
</br></br>
<input type="submit" value="Get Result" name="Submit">
</form>
</td>
</tr><tr>
<td align=center>
<span><?= result ?></span><br>
</td>
</tr>
</table>
</body>
</center>
</html>
RESULT.HTML
<html>
<center>
<head>
<base target="_top">
<style>
input{
width:15%;
border:1px dashed black;
margin:1px;}
input:hover{
background:red;}
table {
font-family: times;
border-collapse:collapse;
width: 30%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;border:1px solid green;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table border="2" width=100% cellspacing="0" >
<tr>
<td>Class:</td>
<td><?= cl ?></td>
</tr><tr>
<th>Roll No:</th>
<th><?= id ?></th>
</tr><tr>
<td>Student name:</td>
<td><?= stn ?></td>
</tr><tr>
<td>Father Name:</td>
<td><?= fa ?></td>
</tr><tr>
<td>Hindi:</td>
<td><?= hi ?></td>
</tr>
<td>English:</td>
<td><?= en ?></td>
</tr>
<td>Math:</td>
<td><?= mt ?></td>
</tr>
<td>Science:</td>
<td><?= sc ?></td>
</tr>
<td>Computer:</td>
<td><?= co ?></td>
</tr>
<td>Total Marks:</td>
<td><?= tm ?></td>
</tr><tr>
<td>Grade:</td>
<td><?= re ?></td>
</tr>
<tr>
<td colspan=2 align=center>
<?var url = getUrl();?>
<form method="post" action="<?= url ?>"> Back to
<input type="submit" value="Home" name="back"></form></td>
</tr>
</table>
</body>
</center>
</html>
ERROR.HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
PAGINA DI ERRORE
Codice originale di: Sunrise iT Education
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
