-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
65 lines (50 loc) · 2.12 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Author: Hugh Downer & Dave Mattey
* Desc: Script for converting data from old WeatherCat format, to new WeatherCat format
* Date: 29/12/2013
*/
/** PHPExcel_IOFactory */
//Include an external library that lets us read in xlsx files
include 'includes/PHPExcel/IOFactory.php';
//load some custom function for use within the script
require 'includes/functions.php';
//load config
require 'includes/config.php';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
echo '<hr />';
echo "<pre>";
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$i = 1; //set a count
$lineNum = 0; //set our lineNumber var now, will begin inc' when required
foreach($sheetData as $row){
if($i == $headerRowLine) $headers = $row; //grab this row, it contains the required header values
if($i > $readDataAfterLine){ //gone past header rows, into data
$lineNum += 10; //inc our line number by 10 (this means lineNumbering STARTS at 10)
foreach($row as $column => $data){
if(preg_match("/[a-zA-Z]:$/",$headers[$column])){ //match up the column data to its header (e.g. B => t:) and assign to the array
$formattedData[$lineNum][$headers[$column]] = $data;
}
$formattedData[$lineNum]['t:'] = formatTimeStamp($row['A'], $row['C']); //special case, for the timestamp
}
}
$i++; //inc the count after each iteration
}
$handle = fopen($outputFileName, "w"); //open our file for writing
if(!$handle){
die("Error opening output at ".$outputFileName." file, check your permissions");
}
//Loop through our formatted data and write to the file
foreach($formattedData as $lineNumber => $data){
$line = "";
foreach($data as $header => $value){
$line .= $header.trim($value)." "; //formats our header:value header:value string
}
fwrite($handle, str_pad($lineNumber, 5, " ", STR_PAD_LEFT)." ".$line.$verificationCode."\r\n"); //write to file, lineNumber is padded left with spaces, as per original file
}
fclose($handle); //close the stream
echo "Completed, file written at: ".$outputFileName;