0% found this document useful (0 votes)
803 views

PMG Panchayat Doc Script

The document contains JavaScript code to convert JSON data to a CSV file and download it. It loops through JSON data, extracts values, and generates CSV rows with column values separated by commas. It then generates a hidden link to trigger the download and passes the CSV data and file name.

Uploaded by

AnilKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
803 views

PMG Panchayat Doc Script

The document contains JavaScript code to convert JSON data to a CSV file and download it. It loops through JSON data, extracts values, and generates CSV rows with column values separated by commas. It then generates a hidden link to trigger the download and passes the CSV data and file name.

Uploaded by

AnilKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

uj = 0;

function makeexcel() {
sdata = Object.values(sdata);

if (uj == 0) {
alert("download");
uj = 1;
JSONToCSVConvertor(sdata, "Vehicle Report", true);
uj = 0;
}
}

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {


//If JSONData is not an object then JSON.parse will parse the JSON string in an
Object
var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;

var CSV = "";


//Set Report title in first row or line

CSV += ReportTitle + "\r\n\n";

//This condition will generate the Label/Header


if (ShowLabel) {
var row = "";

//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ",";
}

row = row.slice(0, -1);

//append Label row with line break


CSV += row + "\r\n";
}

//1st loop is to extract each row


for (var i = 0; i < arrData.length; i++) {
var row = "";

//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}

row.slice(0, row.length - 1);

//add a line break after each row


CSV += row + "\r\n";
}

if (CSV == "") {
alert("Invalid data");
return;
}

//Generate a file name


var fileName = "MyReport_";
//this will remove the blank-spaces from the title and replace it with an
underscore
fileName += ReportTitle.replace(/ /g, "_");

//Initialize file format you want csv or xls


var uri = "data:text/csv;charset=utf-8," + escape(CSV);

// Now the little tricky part.


// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension

//this trick will generate a temp <a /> tag


var link = document.createElement("a");
link.href = uri;

//set the visibility hidden so it will not effect on your web-layout


link.style = "visibility:hidden";
link.download = fileName + ".csv";

//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

function adds(src) {
var my_awesome_script = document.createElement("script");
my_awesome_script.setAttribute("src", src);
document.head.appendChild(my_awesome_script);
}

async function asyncForEach(length, callback) {


for (let index = 0; index < length; index++) {
await callback(index);
}
return Promise.resolve("");
}

var sdata = {};


jsonData = [];

//window.addEventListener(
//"load",
//() => {
areais = $("select[name~='panchayatId']").val();

$("body").append(
`<div
id="getexcel"
onclick="makeexcel()"
style="
position: fixed;
bottom: 0px;
right: 0;
padding: 21px;
background-color: #ff0000;
color: #fbfbfb;
"
>
GET EXCEL <span id="counts"></span>
</div>
`
);
alert("wait some time ");
$("body").append('<div id="form1" style="display:none" ></div>');

var f = 1;
totalData = $("span.totalRecord").text();
totalPage = Math.ceil(totalData / 100);

jsonPromise = new Promise((res, rej) => {


(async () => {
await asyncForEach(totalPage, (page) => {
page++;
return new Promise((res, rej) => {
$.ajax({
url:
"https://www.pmgdisha.in/app/trainingCenter/getStudentSummary?
areaId=" +
areais +
"&pageSize=100" +
"&search=&pageNumber=" +
page,
type: "GET",
contentType: "application/json",
data: {},
dataType: "text",
success: function (data) {
ig = 0;
ip = 0;
try {
jsonData = [...jsonData, ...JSON.parse(data)];
} catch (error) {
//
}
res();
},
});
});
});
res();
})();
});

await jsonPromise;

jsonData.forEach(function (value) {
$.get(
"https://www.pmgdisha.in/app/trainingCenter/editStudent?id=" +
value.userId,

function (data, status) {


try {
$("#form1").html(data);
} catch (error) {
//
}

const useri = value.userId;

sdata[useri] = {};

sdata[useri]["userName"] = value.userName;
sdata[useri]["studentName"] = $("#studentName").val();
sdata[useri]["fatherName"] = $("#fatherName").val();

sdata[useri]["motherName"] = $("#motherName").val();
sdata[useri]["yourdob1"] = $("#yourdob1").val();

sdata[useri]["emailId"] = $("#emailId").val();
sdata[useri]["studentAddress"] = $("#studentAddress").val();

sdata[useri]["mobileNumber"] = $("#mobileNumber").val();
sdata[useri]["village"] = $("#village").val();

$.get(
"https://www.pmgdisha.in/app/trainingCenter/resetStudentPassword?id=" +
useri,
function (data, status) {
console.log(f);
sdata[useri]["password"] = data.newPassword;
$("#counts").html(f);
f++;
ip++;
}
);

$("#counts").html(f);
}
);
});
//},
//false
//);

You might also like