User:Poccil/Schooltables.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Poccil/Schooltables. |
////////////////////////////////////////////
/*
CCD Build a Table CSV parser
For this program to parse CSV files correctly, be sure
the columns of the CSV file are in the following order:
Agency Name- by survey year (District),
School Name- by survey year (School),
State Abbr (School),
County Name (School),
Location City (School),
School Type (School),
Agency Type (District),
Total Students (School),
School Level Code (School),
Low Grade (school),
High Grade (School)
This script will save to the file schools.txt
*/
// replace with filename of CSV file to use
var csvfile="NCES_Report_202783694.csv"
////////////////////////////////////////////
var fso=new ActiveXObject("Scripting.FileSystemObject")
function toCap(x){
return x.replace(/(\w)(\w*)(?=\W|$)/g,function(a,b,c){
return b+c.toLowerCase()
})
}
function parseOrBlank(x){
x=parseInt(x)
return (isNaN(x))?"":x
}
var postal=[
"AK|Alaska",
"AL|Alabama",
"AR|Arkansas",
"AZ|Arizona",
"CA|California",
"CO|Colorado",
"CT|Connecticut",
"DC|District of Columbia",
"DE|Delaware",
"FL|Florida",
"GA|Georgia",
"HI|Hawaii",
"IA|Iowa",
"ID|Idaho",
"IL|Illinois",
"IN|Indiana",
"KS|Kansas",
"KY|Kentucky",
"LA|Louisiana",
"MA|Massachusetts",
"MD|Maryland",
"ME|Maine",
"MI|Michigan",
"MN|Minnesota",
"MO|Missouri",
"MS|Mississippi",
"MT|Montana",
"NC|North Carolina",
"ND|North Dakota",
"NE|Nebraska",
"NH|New Hampshire",
"NJ|New Jersey",
"NM|New Mexico",
"NV|Nevada",
"NY|New York",
"OH|Ohio",
"OK|Oklahoma",
"OR|Oregon",
"PA|Pennsylvania",
"RI|Rhode Island",
"SC|South Carolina",
"SD|South Dakota",
"TN|Tennessee",
"TX|Texas",
"UT|Utah",
"VA|Virginia",
"VT|Vermont",
"WA|Washington",
"WI|Wisconsin",
"WV|West Virginia",
"WY|Wyoming",
"AS|American Samoa",
"BI|Bureau of Indian Affairs",
"DD|Department of Defense (Domestic)",
"DO|Department of Defense (Overseas)",
"GU|Guam",
"MP|Northern Marianas",
"PR|Puerto Rico",
"VI|Virgin Islands"
]
function nameFromPostal(x){
for(var i=0;i<postal.length;i++){
var xx=postal[i].split("|")
if(xx[0]==x)return xx[1]
}
return "[unnamed]"
}
var state
var o=fso.OpenTextFile(csvfile)
var o1=fso.CreateTextFile("schools.txt",2)
var i=0
function SchoolToRow(ln,grd){
var grds=(grd)?" ("+ln[9]+" to "+ln[10]+")":""
return "| "+ln[1]+grds
+" || [["+ln[0]+"]]"
+" || [["+ln[3]+" County, "+ln[2]+"]]"
+" || [["+ln[4]+", "+ln[2]+"]]"
+" || "+ln[7]
+"\r\n|-\r\n"
+'<!--\r\n| align="right" | Notes || colspan="4" | '
+'(replace this with notes and un-comment line)\r\n|-\r\n-->\r\n'
}
function parseSchool(ln){
ln=ln.split(",")
if(ln.length>8){
ln[6]=parseOrBlank(ln[6])//agency type
ln[5]=parseOrBlank(ln[5])//school type
ln[7]=parseOrBlank(ln[7])//total students
ln[8]=parseOrBlank(ln[8])//level type
ln[2]=state=nameFromPostal(ln[2])
ln[9]=ln[9].toLowerCase()
ln[10]=ln[10].toLowerCase()
ln[0]=toCap(ln[0].replace(/\s+$/g,""))
.replace("Sch Dist","School District")
.replace(/Schs$/,"Schools")
.replace("Reg Dist","Regional School District")
ln[1]=toCap(ln[1].replace(/\s+$/g,""))
.replace("E. School","Elementary School")
.replace("El School","Elementary School")
.replace("El. School","Elementary School")
ln[3]=toCap(ln[3].replace(/\s+$/g,""))
ln[4]=toCap(ln[4].replace(/\s+$/g,""))
} else {
return null
}
return ln
}
var schtypes=[[],[],[],[]]
while(!o.AtEndOfStream){
var ln=o.ReadLine().replace(/\s+$/,"")
if(i>=3&&ln){
var sch=parseSchool(ln)
if(sch){
var st=sch[8]-1;
schtypes[st][schtypes[st].length]=sch
}
}
i++
}
o.Close()
function writeSchoolType(o1,row,scht,grd){
o1.write("=="+row+"==\r\n{| border=\"1\"\r\n")
o1.write("! School Name !! District !! County !! City !! Students\r\n|-\r\n")
for(var i=0;i<scht.length;i++){
o1.write(SchoolToRow(scht[i],grd))
}
o1.write("|}\r\n")
}
o1.write("This is a '''list of public schools in [["+state+"]]''' as of the [[2002]]-[[2003]] school year. Student counts are as of 2003.\r\n")
writeSchoolType(o1,"High schools",schtypes[2])
writeSchoolType(o1,"Middle and junior high schools",schtypes[1])
writeSchoolType(o1,"Primary schools",schtypes[0])
writeSchoolType(o1,"Other schools",schtypes[3],1)
o1.close()