Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form
function validateEntry(){

// declare a variable and referernece of active google sheet

var myGoogleSheet = SpreadsheetApp.getActiveSpreadsheet();

var shUserForm = myGoogleSheet.getSheetByName("User Form");
// to create the instance of the user interface to show the alert
var ui = SpreadsheetApp.getUi();

shUserForm.getRange("C3").setBackground('#FFFFF');
shUserForm.getRange("C5").setBackground('#FFFFF');
shUserForm.getRange("C7").setBackground('#FFFFF');
shUserForm.getRange("C9").setBackground('#FFFFF');
shUserForm.getRange("C11").setBackground('#FFFFF');
shUserForm.getRange("C13").setBackground('#FFFFF');
shUserForm.getRange("C15").setBackground('#FFFFF');
shUserForm.getRange("C17").setBackground('#FFFFF');
shUserForm.getRange("C19").setBackground('#FFFFF');
shUserForm.getRange("C21").setBackground('#FFFFF');

// validating Division
if(shUserForm.getRange("C3").isBlank()==true){
  ui.alert("Please enter Division");
  shUserForm.getRange("C3").activate();
  shUserForm.getRange("C3").setBackground('#FF0000');
  return false;
}

// validating Section
if(shUserForm.getRange("C5").isBlank()==true){
  ui.alert("Please enter Section");
  shUserForm.getRange("C5").activate();
  shUserForm.getRange("C5").setBackground('#FF0000');
  return false;
}

// validating Station
if(shUserForm.getRange("C7").isBlank()==true){
  ui.alert("Please enter Station");
  shUserForm.getRange("C7").activate();
  shUserForm.getRange("C7").setBackground('#FF0000');
  return false;
}

// validating inspected by
if(shUserForm.getRange("C9").isBlank()==true){
  ui.alert("Please enter Officer Name");
  shUserForm.getRange("C9").activate();
  shUserForm.getRange("C9").setBackground('#FF0000');
  return false;
}

// validating date

if(shUserForm.getRange("C11").isBlank()==true){
  ui.alert("Please enter Date");
  shUserForm.getRange("C11").activate();
  shUserForm.getRange("C11").setBackground('#FF0000');
  return false;
}

// validating major deficiencies 1
if(shUserForm.getRange("C13").isBlank()==true){
  ui.alert("Please enter  deficiency 1");
  shUserForm.getRange("C13").activate();
  shUserForm.getRange("C13").setBackground('#FF0000');
  return false;
}

/* validating major deficiencies 2
if(shUserForm.getRange("C15").isBlank()==true){
  ui.alert("Please enter atlease deficiency 2");
  shUserForm.getRange("C15").activate();
  shUserForm.getRange("C15").setBackground('#FF0000');
  return false;
}

// validating major deficiencies 3
if(shUserForm.getRange("C17").isBlank()==true){
  ui.alert("Please enter deficiency 3");
  shUserForm.getRange("C17").activate();
  shUserForm.getRange("C17").setBackground('#FF0000');
  return false;
}

// validating major deficiencies 4
if(shUserForm.getRange("C19").isBlank()==true){
  ui.alert("Please enter deficiency 4");
  shUserForm.getRange("C19").activate();
  shUserForm.getRange("C19").setBackground('#FF0000');
  return false;
}

// validating major deficiencies 5
if(shUserForm.getRange("C21").isBlank()==true){
  ui.alert("Please enter deficiency 5");
  shUserForm.getRange("C21").activate();
  shUserForm.getRange("C21").setBackground('#FF0000');
  return false;
}*/
  return true;
}

// function to submit the data
function submitData(){
  // declare a variable and set the reference of active google sheet

  var myGoogleSheet = SpreadsheetApp.getActiveSpreadsheet();

  var shUserForm = myGoogleSheet.getSheetByName("User Form");

  var datasheet = myGoogleSheet.getSheetByName("Database");

// to create the instance of ther user-interface enviroment to user the alert feature

var ui = SpreadsheetApp.getUi();

var response = ui.alert("Submit", "Do you want to submit the data?", ui.ButtonSet.YES_NO);

// checking the user response

if(response == ui.Button.No){
  alert.ui("no data saved");
  return;// to exit from this function
}

if(validateEntry() == true){

 
  var blankRow = datasheet.getLastRow()+1; // identify the next blank row

  //code to update the database sheet
  //For division
  datasheet.getRange(blankRow,1).setValue(shUserForm.getRange("C3").getValue());
  //For section
  datasheet.getRange(blankRow,2).setValue(shUserForm.getRange("C5").getValue());
  //For station
  datasheet.getRange(blankRow,3).setValue(shUserForm.getRange("C7").getValue());
  //For officer name
  datasheet.getRange(blankRow,4).setValue(shUserForm.getRange("C9").getValue());
  //For date
  datasheet.getRange(blankRow,5).setValue(shUserForm.getRange("C11").getValue());
  //For major deficiency 1
  datasheet.getRange(blankRow,6).setValue(shUserForm.getRange("C13").getValue());
  //For major deficiency 2
  datasheet.getRange(blankRow,7).setValue(shUserForm.getRange("C15").getValue());
  //For major deficiency 3
  datasheet.getRange(blankRow,8).setValue(shUserForm.getRange("C17").getValue());
  //For major deficiency 4
  datasheet.getRange(blankRow,9).setValue(shUserForm.getRange("C19").getValue());
  //For major deficiency 5
  datasheet.getRange(blankRow,10).setValue(shUserForm.getRange("C21").getValue());
 

      ui.alert("Entry saved successfully");

shUserForm.getRange("C3").clear();
shUserForm.getRange("C5").clear();
shUserForm.getRange("C7").clear();
shUserForm.getRange("C9").clear();
shUserForm.getRange("C11").clear();
shUserForm.getRange("C13").clear();
shUserForm.getRange("C15").clear();
shUserForm.getRange("C17").clear();
shUserForm.getRange("C19").clear();
shUserForm.getRange("C21").clear();

}



}







Train Reservation Booking Application

package com.java.practiceQuestion;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URI;

import java.net.http.HttpClient;

import java.net.http.HttpRequest;

import java.net.http.HttpResponse;

import java.util.Scanner;


public class APIMain {

// =================================================================================================================//

// method to search train using train number

public static void searchTrain() {

// getting bufferedReader instance to take input from user

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

// printing on console

System.out.println("Enter the train number");

// taking input from user

int trainNumber = 0;

try {

trainNumber = Integer.parseInt(bufferedReader.readLine());

} catch (NumberFormatException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

// building the complete url by adding train number with it.

String urlString = "https://irctc1.p.rapidapi.com/api/v1/searchTrain?query=" + trainNumber;

// Http Request : The request sent by the computer to a web server, contains all

// sorts of potentially interesting information

// The HTTP request method indicates the method to be performed on the resource

// identified by the Requested URI (Uniform Resource Identifier).

// build : Builds and returns an HttpRequest.

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString))

.header("X-RapidAPI-Key", "e8094312d4msh5b6888fbfd812d5p1cb25djsn92dd6f76700c")

.header("X-RapidAPI-Host", "irctc1.p.rapidapi.com")

.method("GET", HttpRequest.BodyPublishers.noBody())

.build();

// creating and initializing an variable of HttpResponse type

HttpResponse<String> response = null;

// try-catch block to catch the exception

try {

// HttpClient is used to send requests and retrieve their responses

response = HttpClient.newHttpClient().

send(request, HttpResponse.BodyHandlers.ofString());

// catch block to catch the exception if exists

} catch (IOException | InterruptedException e) {

// This method prints a stack trace for this Throwable object on the error

// output stream that is the value of the field System.err. The first line

// of output contains the result of the toString() method for this object.

// Remaining lines represent data previously recorded by the method

// fillInStackTrace().

e.printStackTrace();

}

// printing the response on console

System.out.println(response.body());

}

// =================================================================================================================//


// method to search station using initials of any stations of Indian railways

public static void searchStation() {

// getting the instance of buffered reader to take input from user

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

// printing on the console

System.out.println("Enter the first initials of any valid Indian Railways station name");

// variable to store station name

String stationName = null;

// try block

try {

// taking input from user;

stationName = bufferedReader.readLine().toUpperCase();

// catch block

} catch (IOException e1) {

e1.printStackTrace();

}

// building the complete url by adding train number with it.

String urlString = "https://irctc1.p.rapidapi.com/api/v1/searchTrain?query=" + stationName;

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString))

.header("X-RapidAPI-Key", "e8094312d4msh5b6888fbfd812d5p1cb25djsn92dd6f76700c")

.header("X-RapidAPI-Host", "irctc1.p.rapidapi.com")

.method("GET", HttpRequest.BodyPublishers.noBody())

.build();

HttpResponse<String> response = null;

try {

response = HttpClient.newHttpClient()

.send(request, HttpResponse.BodyHandlers.ofString());

} catch (IOException | InterruptedException e) {

e.printStackTrace();

}

System.out.println(response.body());

}

// =================================================================================================================//


// method to find all trains between two stations

public static void trainsBetweenStations() {

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the name of boarding station");

String boardingStation = null;

try {

boardingStation = bufferedReader.readLine();

} catch (IOException e1) {


e1.printStackTrace();

}

System.out.println("Enter the name of destination station");

String destStation = null;

try {

destStation = bufferedReader.readLine();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

// building the complete url by adding train number with it

String urlString = "https://irctc1.p.rapidapi.com/api/v2/trainBetweenStations?fromStationCode="

+ boardingStation + "&toStationCode=" + destStation;

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString))

.header("X-RapidAPI-Key", "e8094312d4msh5b6888fbfd812d5p1cb25djsn92dd6f76700c")

.header("X-RapidAPI-Host", "irctc1.p.rapidapi.com")

.method("GET", HttpRequest.BodyPublishers.noBody())

.build();

HttpResponse<String> response = null;

try {

response = HttpClient.newHttpClient()

.send(request, HttpResponse.BodyHandlers.ofString());

} catch (IOException | InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(response.body());

}


// =================================================================================================================//

// method to find the train live status

public static void trainLiveStatus() {

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the train number");

int trainNumber = 0;

try {

trainNumber = Integer.parseInt(bufferedReader.readLine());

} catch (NumberFormatException | IOException e1) {


e1.printStackTrace();

}

// building the complete url by adding train number with it.

String urlString = "https://irctc1.p.rapidapi.com/api/v1/searchTrain?query=" + trainNumber + "&startDay=1";

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString))

.header("X-RapidAPI-Key", "e8094312d4msh5b6888fbfd812d5p1cb25djsn92dd6f76700c")

.header("X-RapidAPI-Host", "irctc1.p.rapidapi.com")

.method("GET", HttpRequest.BodyPublishers.noBody())

.build();

HttpResponse<String> response = null;

try {

response = HttpClient.newHttpClient()

.send(request, HttpResponse.BodyHandlers.ofString());

} catch (IOException | InterruptedException e) {

e.printStackTrace();

}

System.out.println(response.body());

}

// =================================================================================================================//


// method to get the PNR status of any train using PNR number

public static void getPNRStatus() {

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the PNR Number");

String pnrString = null;

try {

pnrString = bufferedReader.readLine();

} catch (IOException e1) {

e1.printStackTrace();

}

String urlString = "https://irctc1.p.rapidapi.com/api/v3/getPNRStatus?pnrNumber=" + pnrString;

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString))

.header("X-RapidAPI-Key", "e8094312d4msh5b6888fbfd812d5p1cb25djsn92dd6f76700c")

.header("X-RapidAPI-Host", "irctc1.p.rapidapi.com")

.method("GET", HttpRequest.BodyPublishers.noBody())

.build();

HttpResponse<String> response = null;

try {

response = HttpClient.newHttpClient()

.send(request, HttpResponse.BodyHandlers.ofString());

} catch (IOException | InterruptedException e) {

e.printStackTrace();

}

System.out.println(response.body());

}


// =================================================================================================================//

// driver method

public static void main(String[] args) throws IOException, InterruptedException {

// try block

try (Scanner scanner = new Scanner(System.in)) {

// printing on the console

System.out.println("Welcome to Indian Railways");

System.out.println("1. Press 1 to search the train using train number");

System.out.println("2. Press 2 to seach stations using initials of station");

System.out.println("3. Press 3 to get all trains between two stations");

System.out.println("4. Press 4 to get live running train status");

System.out.println("5. Press 5 to get PNR status");

System.out.println("6. Press 6 to exit");

// infinite loop

while (true) {

// taking input from user

int button = scanner.nextInt();

switch (button) {

// witch statement : calling respective methods according to user's input

case 1: // to search the train using train number

searchTrain();


break;

case 2: // to search stations using initials of station

searchStation();


break;

case 3: // to get all trains between two stations

trainsBetweenStations();


break;

case 4: // to get live running train status

trainLiveStatus();


break;

case 5: // to get PNR status

getPNRStatus();


break;


case 6: // to exit from the program

System.out.println("Thankyou for using the app!!!");

System.exit(button);

break;

}


}

}

}


}


Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...