constDATA_ENTRY_SHEET_NAME="Registration";constTIME_STAMP_COLUMN_NAME="Timestamp";// You can edit the name of this column name or you can put blank like this : "". Ensure that the same name exist there in the sheet as well.varsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(DATA_ENTRY_SHEET_NAME);constdoPost=(request={})=>{const{postData:{contents,type}={}}=request;vardata=parseFormData(contents);try{appendToGoogleSheet(data);}catch(e){}try{send_confirmation_mail();}catch(e){}returnContentService.createTextOutput(contents).setMimeType(ContentService.MimeType.JSON);};functionsend_confirmation_mail(data){url="https://btf.pythonanywhere.com/send-registration-confirmation?n="+data["Name"].replace(" ","+")+"&i="+data["Institution"].replace(" ","+")+"&e="+data["Email"].replace(" ","");UrlFetchApp.fetch(url);}functionparseFormData(postData){vardata=[];varparameters=postData.split('&');for(vari=0;i<parameters.length;i++){varkeyValue=parameters[i].split('=');data[keyValue[0]]=decodeURIComponent(keyValue[1]);}returndata;}functionappendToGoogleSheet(data){if(TIME_STAMP_COLUMN_NAME!==""){data[TIME_STAMP_COLUMN_NAME]=newDate();}varheaders=sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];varrowData=headers.map(headerFld=>data[headerFld]);sheet.appendRow(rowData);}
constAPI_LINK="https://script.google.com/macros/s/AKfycbz_FzYhH1h0WZIhvpLicgxWQxqpFnkUGAvLN-oTUdMkImJe_hWqDlaQT8GdPn5MPsVmVA/exec";constTechnofest=()=>{consthandleSubmit=(event)=>{event.preventDefault();document.getElementById("message").textContent="Submitting..";document.getElementById("message").style.display="block";document.getElementById("submit-button").disabled=true;// Collect the form datavarformData=newFormData(event.target);varkeyValuePairs=[];for(varpairofformData.entries()){keyValuePairs.push(pair[0]+"="+pair[1]);}varformDataString=keyValuePairs.join("&");// Send a POST request to your Google Apps Scriptfetch(API_LINK,{redirect:"follow",method:"POST",body:formDataString,headers:{"Content-Type":"text/plain;charset=utf-8",},}).then(function(response){// Check if the request was successfulif(response){returnresponse;// Assuming your script returns JSON response}else{thrownewError("Failed to submit the form.");}}).then(function(data){// Display a success messagedocument.getElementById("message").textContent="Data submitted successfully!";document.getElementById("message").style.display="block";document.getElementById("message").style.backgroundColor="green";document.getElementById("message").style.color="beige";document.getElementById("submit-button").disabled=false;event.target.reset();setTimeout(function(){document.getElementById("message").textContent="";document.getElementById("message").style.display="none";},2600);}).catch(function(error){// Handle errors, you can display an error message hereconsole.error(error);document.getElementById("message").textContent="An error occurred while submitting the form."+": "+error;document.getElementById("message").style.display="block";});};
Last Updated: 2024-05-12 ; Contributors: AhmedThahir