Automations and Fees management in apps script
This page is a part of Student registration form, Both projects work on same sheet. The projects are based on School management system.
The getDetail
function retrieves data based on the input "form.id". It searches the sheet named "alldatastore" for a match with "code" (constructed as "E" + form.id). If a match is found, the function retrieves various data from different columns in the Diary sheet and stores them in separate variables. The function also retrieves data from two other sheets (PData and Ptra) and creates a table "wr" with the retrieved data and returns it.
The code defines an apps script function called "pPro" that is used to process a form. The function performs several tasks including:
- creating a unique transaction ID (tid) using a zero-padded number
- extracting values from the form (ac, cl, sn, pa, ma, nt) and storing them in variables
- getting the current date and time and storing them in variables (d, mtn, dt, mt, yt, th, tm, ts, stm, std)
- appending a row to a sheet called "wsT" with the extracted values and calculated date/time
- looking up the user's account balance in a sheet called "wsP" and updating the balance
- sending a text message to the user's phone number with the updated balance.
This is an HTML page with styling information, some JavaScript functions, and form elements. The page is designed to display student information after fetching the data from an external source with the help of Google Script and jQuery. The page contains form elements like input boxes and text areas and also has styles defined for different HTML elements like tables, buttons, and forms. Some of the JavaScript functions are used to manipulate the data being displayed, prevent form submissions, and show a loading message while fetching the data.
<html> <head> <base target="_top"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <style> tr:nth-child(odd){background:#cece ;} tr:nth-child(even){background: #efe} td{border:0px solid lightpink;} .yty{border-top:1px solid white;} table,td{border-collapse:collapse;font-family:times;font-size:12px;border-radius:5px;border:yes;} td[class=b]{text-transform:uppercase;font-weight:bold;} input,textarea{font-family:arbic;font-size:12px;border:1px dashed green;} .tmn{margin-top:0.01em;} ay{ background: red; font-size: 12px; display: inline-block; color: white; padding: 2px 5px; cursor: pointer; border:1px solid black; border-radius: 10px; } img{border:none;} #box{ width: 250px; overflow: hidden; box-shadow: 0 0 10px green; border-radius: 10px; position: absolute; top: 20%; left: 50%; background: white; transform: translate(-50%, -50%); z-index: 9999; padding: 10px; text-align: center; display: none; }body{width:60%;margin-left:200px;margin-top:20px;} </style> <script> function cp() { var zd = (num, places) => String(num).padStart(places, '0') var pd = document.getElementById('iptv').value; document.getElementById('id').value = zd(pd, 6); } function preventFormSubmit() { var forms = document.querySelectorAll('form'); for (var i = 0; i < forms.length; i++) { forms[i].addEventListener('submit', function(event) { event.preventDefault(); }); } } window.addEventListener('load', preventFormSubmit); function SubmitID(form) { var idc = document.getElementById("id").value; if (idc === "") { document.getElementById('loader').innerHTML = "<b style=color:red;>Enter enrollment number"; }else{ document.getElementById('loader').innerHTML = 'Getting information...'; google.script.run.withFailureHandler(onFailure) .withSuccessHandler(feed) .getDetail(form);}} function feed(data) { var idc = document.getElementById("id").value; if ("E"+idc == data[0]) { document.getElementById('loader').innerHTML ="Detail of ID: <b style=color:green;>"+data[0]; document.getElementById('dih').innerHTML =data[0]; document.getElementById('ac').value =data[0]; document.getElementById('cl').value =data[2]; document.getElementById('img').src =data[1]; document.getElementById('cls').innerHTML ="ID NO: "+data[0]; document.getElementById('sname').innerHTML =data[3]+" Class:"+data[2]; document.getElementById('sn').value =data[3]; document.getElementById('fn').innerHTML =data[4]; document.getElementById('vil').innerHTML =data[5]; document.getElementById('Jan').innerHTML =data[6]; document.getElementById('Feb').innerHTML =data[7]; document.getElementById('Mar').innerHTML =data[8]; document.getElementById('Apr').innerHTML =data[9]; document.getElementById('May').innerHTML =data[10]; document.getElementById('Jun').innerHTML =data[11]; document.getElementById('Jul').innerHTML =data[12]; document.getElementById('Aug').innerHTML =data[13]; document.getElementById('Sep').innerHTML =data[14]; document.getElementById('Oct').innerHTML =data[15]; document.getElementById('Nov').innerHTML =data[16]; document.getElementById('Dec').innerHTML =data[17]; document.getElementById('sf').innerHTML =data[18]; document.getElementById('tch').innerHTML =data[19]; document.getElementById('och').innerHTML =data[20]; document.getElementById('Monthly').innerHTML = data[21]; document.getElementById('ppd').innerHTML =data[22]; document.getElementById('pm').innerHTML =data[23]; document.getElementById('bs').innerHTML =data[25]+" Rs."+data[24]; document.getElementById('pa').value =data[24]; document.getElementById('anu').innerHTML =data[26]; document.getElementById('od').innerHTML =data[27]; document.getElementById('pyd').innerHTML =data[28]; document.getElementById('psm').innerHTML =data[30]; document.getElementById('msp').innerHTML =data[31]; document.getElementById("trd").innerHTML = data[29];; document.getElementById("ttd").innerHTML = data[32]; if (data[28]>0){ var spy = "Dues";}else{var spy = "Advance";}; document.getElementById('psy').innerHTML=spy; var tP = 0; $("td.ptp").each(function(i, td){ tP += parseInt($(td).text());}); var tm = 0; $("td.ptm").each(function(i, td){ tm += parseInt($(td).text());}); var pys = data[28]+tP+tm; $('#pttp').html("Had to pay:₹"+pys+"- Paid:₹"+tP+"- Discount:₹"+tm+"=₹"+data[28]+"(Added as PY"+spy+")"); }else{ document.getElementById('loader').innerHTML = "<b style=color:red;>"+data[0]; }} function onFailure(error){ document.getElementById('loader').innerHTML = error; }; window.addEventListener('load', preventFormSubmit); function pfun(form) { var idc = document.getElementById("ac").value; if (idc === "") { document.getElementById('loader').innerHTML = "<b style=color:red;>Fetch detail to proceed."; document.getElementById("box").style.display = "none"; }else{ document.getElementById('loader').innerHTML = 'Proceeding...'; document.getElementById("box").style.display = "none"; google.script.run.withFailureHandler(onFailure) .withSuccessHandler(pfeed) .pPro(form);}} function pfeed(data) { document.getElementById('pf').reset(); document.getElementById('loader').innerHTML =data[0]; document.getElementById('ppd').innerHTML =data[1]; document.getElementById('pm').innerHTML =data[2]; document.getElementById('bs').innerHTML =data[4]+" Rs."+data[3]; document.getElementById('pa').value =data[3]; document.getElementById('trd').innerHTML =data[5];} </script>
</head> <body > <div id='divid'> <table width=100% id=tb> <tr> <td width="230px"> <form onsubmit="SubmitID(this)"> <input type="number"onKeyUp="cp()"id="iptv"Placeholder="ID number"style="width:100px"> <input id="id"name="id"hidden> <input type="submit" value="Get Detail"style="background-color:green;color:white;border:1px dashed white;font-size: 12px; cursor: pointer;"></form> </td> <td id="loader"> </td> </tr> <tr> <td colspan=2> <table width=100%> <tr> <td rowspan=4 width=110px align=center><img id="img" height=110px width=105px> </td> <td id="cls" width=90px align="center"style="background-color:yellow;font-weight:bold;"></td> <td id="sname"class="b">.</td> <td rowspan=3 width=300px> <table width=100%> <tr> <td>Jan:<b id="Jan"></b></td> <td>Feb:<b id="Feb"></b></td> <td>Mar:<b id="Mar"></b></td> <td>Apr:<b id="Apr"></b></td> </tr> <tr> <td>May:<b id="May"></b></td> <td>Jun:<b id="Jun"></b></td> <td>Jul:<b id="Jul"></b></td> <td>Aug:<b id="Aug"></b></td> </tr> <tr> <td>Sep:<b id="Sep"></b></td> <td>Oct:<b id="Oct"></b></td> <td>Nov:<b id="Nov"></b></td> <td>Dec:<b id="Dec"></b></td> </tr> </table> </td> </tr> <tr> <td width=70px>F-Name:</td> <td class=b id="fn" width=200px></td> </tr> <tr><td>Village:</td> <td class=b id="vil"></td></tr> <tr><td colspan=3 align="center"> <table width=100% class=tmn> <tr class=it> <td width=25%>School-Fee:<b id="sf"></b></td> <td width=25%>Trans-Ch.:<b id="tch"></b></td> <td width=25%>OtherCh.:<b id="och"></b></td> <td width=25% style="background-color:yellow;">Monthly:<b id="Monthly"></b></td> </tr> <tr> <td >PY<i id=psy></i> Rs:<b id="pyd"></b></td> <td>Annual-Fee:<b id="anu"></b></td> <td>Other Dues:<b id="od"></b></td> <td>PM <b id="psm"></b>:<b id="msp"></b></td> </tr> <tr> <td colspan=2>Total till now:Paid Rs:<b id="ppd"></b> & Margin Rs:<b id="pm"></b></td> <td style="background-color:yellow;"><b id="bs"></b></td> <td align=center style="background-color:red;color:white; cursor: pointer;" onclick="pop()">Proceed to Pay</td> </tr> </table> </td></tr> </table> </td> </tr> </table> <div id="box"> <i class="fas fa-check-circle"></i> <form id="pf" onsubmit="pfun(this)"> <table> <tr> <th colspan=2>Pay/Margin to <b id="dih"style="color:green;"></b></th> </tr> <tr> <td width=70px>Pay Rs: <input id="ac"name="ac"hidden> <input id="cl"name="cl"hidden> <input id="sn"name="sn"hidden></td> <td><input name="pa"id="pa"type="number" placeholder="Enter ammount"></td> </tr> <tr> <td width=70px>Discount:</td> <td ><input name="ma"type="number" placeholder="Enter ammount"></td> </tr> <tr> <td width=70px>Note:</td> <td ><textarea name="nt"id="nt"type="text" placeholder="Note"></textarea> </td> </tr> <tr> <td width=70px></td> <td align=center><input type="submit" value="Submit"onsubmit="pfun(this)"style="background-color:green;color:white;border-radius:5px;font-size: 10px;"> <a style="background-color:red;color:white;border:1px solid black;border-radius:5px;font-size: 12px; cursor: pointer;" onclick="pop()"> Cancel </a></td> </tr> </table> </form> </div> <div id=trd></div> <div id=ttd></div></div> <center><input type="button" name="btnprint" value="Print" onclick="Print('divid')"></center>
<script language="javascript"> function Print(DivID) { var iPrint = document.getElementById(DivID).innerHTML; var docprint=window.open("",""); docprint.document.open(); docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'); docprint.document.write('<head><meta name="viewport" content="width=device-width, initial-scale=1.0">'); docprint.document.write('<title>Print</title>'); docprint.document.write('<style type="text/css">@page {size:auto;margin:0.2em;}body{'); docprint.document.write('font-family:times; }'); docprint.document.write('tr:nth-child(odd){background: #cece;}tr:nth-child(even){background: #efe}td{border:0px solid lightpink;}.yty{border-top:1px solid white;}table{border-collapse:collapse;font-family:times;font-size:12px;border-radius:5pxborder:none;}td[class=b]{text-transform:uppercase;font-weight:bold;}input{font-family:times;font-size:11px;margin:0.3em;}.tmn{margin-top:0.01em;}'); docprint.document.write('input{display:none;}#box{display:none;}'); docprint.document.write('table{ page-break-inside:auto }tr{ page-break-inside:avoid; page-break-after:auto}thead{ display:table-header-group }tfoot { display:table-footer-group }table{border-collapse: collapse;} #tt{margin-bottom:0.2em;margin-top:0.2em;}</style>'); docprint.document.write('</head><center><body onLoad="self.print()">'); docprint.document.write(iPrint); docprint.document.write('</body><font face=verdana size=1px>℗Power by CodyLab Technology.</font></center></html>'); docprint.document.close(); docprint.focus(); } </script> <script> var modal = null function pop() { if(modal === null) { document.getElementById("box").style.display = "block"; modal = true} else { document.getElementById("box").style.display = "none"; modal = null}} </script> </body> </html>
This script is written in Google Apps Script (a JavaScript-based scripting language) and is used to manage fees in a Google Spreadsheet. The script opens a spreadsheet by its ID using SpreadsheetApp.openById()
method, and accesses the "Diary" sheet using the getSheetByName()
method.
It then creates a new date object using var d1 = new Date()
and obtains the year, month, date, hours, and minutes from it. The script then uses if-else statements to check for different conditions, such as if it is the last month of the year or if the year and month match a specific value.
If the condition is met, the script performs different actions such as writing monthly or yearly fees to the sheet, updating the date and time of when the fees were added, clearing certain cells, and saving the previous year's transactions to another sheet. The fees are added to the sheet by copying values from specific columns and pasting them into other columns.
automate.gs //function to manage fee function writeFees() { var wsm = SpreadsheetApp.openByID("-------ambC6Y2NdeDE------------"); var ws = wsm.getSheetByName("Diary"); var d1 = new Date(); var tn = d1.toLocaleString([],{ hour: '2-digit', minute: '2-digit',second: '2-digit' }); var yd = d1.getFullYear(); var ly = d1.getFullYear()+1; var md = d1.getMonth()+1; var lm = d1.getMonth()+2; var dd = d1.getDate(); var dh = d1.getHours(); var dm = d1.getMinutes(); if(md == "12"){ var yv = d1.getFullYear()+1;}else{var yv = d1.getFullYear();} var ld = new Date(yv,lm, 0).getDate();//last date of next month. var qt = md+","+dd+","+dh+","+dm; //condition to match var t1 = lm+","+ld+","+dh+","+dm; //value1 var t2 = ly+","+md+","+dd+","+dh+","+dm;//value2 var con1 = ws.getRange(1,28).getValue(); var con2 = ws.getRange(1,27).getValue(); var qt1 = ws.getRange(1,30).getValue(); var qt2 = ws.getRange(1,31).getValue(); //condition for write monthly dues if(con1 === "Yes" && qt >= qt1){ var vTc = ws.getRange(3, 17, ws.getLastRow(), 1).getValues(); ws.getRange(3,ws.getLastColumn()-20,vTc.length, 1).setValues(vTc); var rt1 = d1.toDateString(); ws.getRange(1,23).setValue(rt1+", "+tn); ws.getRange(1,30).setValue(t1); ws.getRange(2,21).setValue("");//clear notes //condition for write yearly dues }if(con1 === "Yes" && yd+","+qt >= qt2){ //saving old records and cleaning for new entry var sw = wsm.getSheetByName("PData"); var tw = wsm.getSheetByName("Ptra"); var swr = sw.getDataRange(); var swv = swr.getValues(); var rl = swv.length; var cl = swv[0].length; var twr = tw.getRange(1,1,rl,cl); twr.setValues(swv);//save previous year transactions sw.getRange(3,1,rl,cl).clearContent();//clear sheet PData ws.getRange(3,26,rl,37).clearContent();// clear months fields for new entry ws.getRange(2,21).setValue("Annual fee added.");//add annual fee var py = d1.getFullYear()-1; var tyd = yd.toString().substr(-2); tw.getRange(1,1).setValue(py+"-"+tyd);//set section var vTcy = ws.getRange(3, 17, ws.getLastRow(), 1).getValues(); ws.getRange(3,ws.getLastColumn()-14,vTcy.length, 1).setValues(vTcy); var rt2 =d1.toDateString(); ws.getRange(1,24).setValue(rt2+", "+tn); ws.getRange(1, 31).setValue(t2); //condition for write fees }if(con2 === "Yes"){ var mc =[4,3,2,13,12,11,10,09,08,07,06,05]; var mn = mc[md-1];//month column as in sheet var vTcf = ws.getRange(3, 12, ws.getLastRow(), 1).getValues(); ws.getRange(3,ws.getLastColumn()-mn,vTcf.length,1).setValues(vTcf); var result =d1.toDateString(); ws.getRange(1,25).setValue(result+", "+tn); var range = ws.getRange(23,39);range.setNumberFormat('@STRING@'); }if(con2 === "No"){ var result ="Function is off."; ws.getRange(1,25).setValue(result); } }
Note: You have to add tringger of 5 minutes to automade your sheet. You have to use same sheet with same column length to work correctly.