Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
////////////////////////////////////////////
/*

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()