Skip to content

Commit fdb3831

Browse files
committed
demo-merge-csv-use-php-fputcsv
1 parent 9fbfed9 commit fdb3831

File tree

9 files changed

+264
-0
lines changed

9 files changed

+264
-0
lines changed

demo-convert-to/demo-merge-csv-use-paste/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,24 @@ merge: to-csv
1212
to-csv:
1313
@./to-csv.sh
1414
.PHONY: to-csv
15+
16+
17+
clean:
18+
rm -f *.csv
19+
rm -f final.ods
20+
.PHONY: clean
21+
22+
23+
final-view-csv:
24+
localc ./final.csv
25+
.PHONY: final-view-csv
26+
27+
28+
final-view-ods:
29+
localc ./final.ods
30+
.PHONY: final-view-ods
31+
32+
33+
final-dump-csv:
34+
cat ./final.csv
35+
.PHONY: final-dump-csv
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.csv
2+
final.ods
Binary file not shown.
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
default: merge
4+
.PHONY: default
5+
6+
7+
merge: to-csv
8+
@./merge.sh
9+
.PHONY: merge
10+
11+
12+
to-csv:
13+
@./to-csv.sh
14+
.PHONY: to-csv
15+
16+
17+
clean:
18+
rm -f *.csv
19+
rm -f final.ods
20+
.PHONY: clean
21+
22+
23+
final-view-csv:
24+
localc ./final.csv
25+
.PHONY: final-view-csv
26+
27+
28+
final-view-ods:
29+
localc ./final.ods
30+
.PHONY: final-view-ods
31+
32+
33+
final-dump-csv:
34+
cat ./final.csv
35+
.PHONY: final-dump-csv
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
## Test
4+
5+
執行
6+
7+
``` sh
8+
$ make
9+
```
10+
11+
12+
## Reference
13+
14+
* http://php.net/manual/en/function.fputcsv.php
15+
* http://php.net/manual/en/function.fgetcsv.php
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
abstract class FileCsv {
5+
protected $_FilePath = NULL;
6+
public function getFilePath ()
7+
{
8+
return $this->_FilePath;
9+
}
10+
public function setFilePath ($val)
11+
{
12+
$this->_FilePath = $val;
13+
return $this;
14+
}
15+
16+
protected $_Handle = NULL;
17+
public function getHandle ()
18+
{
19+
return $this->_Handle;
20+
}
21+
public function setHandle ($val)
22+
{
23+
$this->_Handle = $val;
24+
return $this;
25+
}
26+
27+
protected $_Data = array();
28+
public function getData ()
29+
{
30+
return $this->_Data;
31+
}
32+
public function setData ($val)
33+
{
34+
$this->_Data = $val;
35+
return $this;
36+
}
37+
38+
public function toArray ()
39+
{
40+
return $this->getData();
41+
}
42+
43+
public function open ()
44+
{
45+
return $this;
46+
}
47+
48+
public function close ()
49+
{
50+
fclose($this->getHandle());
51+
return $this;
52+
}
53+
54+
55+
56+
}
57+
58+
class FileCsvReader extends FileCsv {
59+
60+
public function open ()
61+
{
62+
if (($this->_Handle = fopen($this->getFilePath(), 'r')) === FALSE) {
63+
return;
64+
}
65+
66+
return $this;
67+
}
68+
69+
public function load ($length=0, $delimiter=',', $enclosure='"', $escape="\\")
70+
{
71+
$this->open();
72+
73+
while (($cols = $this->readRow()) !== FALSE) {
74+
array_push($this->_Data, $cols);
75+
}
76+
77+
return $this;
78+
}
79+
80+
public function readRow ($length=0, $delimiter=',', $enclosure='"', $escape="\\")
81+
{
82+
// http://php.net/manual/en/function.fgetcsv.php
83+
return fgetcsv($this->getHandle(), $length, $delimiter, $enclosure, $escape);
84+
}
85+
86+
87+
}
88+
89+
class FileCsvWriter extends FileCsv {
90+
91+
public function open ()
92+
{
93+
if (($this->_Handle = fopen($this->getFilePath(), 'w')) === FALSE) {
94+
return;
95+
}
96+
97+
return $this;
98+
}
99+
100+
public function save ($delimiter=',', $enclosure='"', $escape="\\")
101+
{
102+
$this->open();
103+
104+
foreach ($this->getData() as $cells) {
105+
$this->writeRow($cells, $delimiter, $enclosure, $escape);
106+
}
107+
108+
return $this;
109+
}
110+
111+
public function writeRow ($cells=array(), $delimiter=',', $enclosure='"', $escape="\\")
112+
{
113+
// http://php.net/manual/en/function.fputcsv.php
114+
return fputcsv($this->getHandle(), $cells, $delimiter=',', $enclosure='"', $escape="\\");
115+
}
116+
117+
}
118+
119+
120+
class AppMerge {
121+
protected $_Csv = array();
122+
123+
public function run ()
124+
{
125+
$this->_Csv['source'] = (new FileCsvReader)
126+
->setFilePath(__DIR__ . '/1.csv')
127+
->load()
128+
;
129+
130+
$data_source = $this->_Csv['source']->toArray();
131+
132+
$this->_Csv['target'] = (new FileCsvReader)
133+
->setFilePath(__DIR__ . '/3.csv')
134+
->load()
135+
;
136+
137+
$data_target = $this->_Csv['target']->toArray();
138+
139+
140+
//var_dump($data_source);
141+
//var_dump($data_target);
142+
143+
foreach ($data_target as $row_target => $cells_target) {
144+
145+
if ($row_target === 0) { // 不處理第一列
146+
continue;
147+
}
148+
149+
foreach ($cells_target as $col_target => $cell_target) {
150+
151+
if ($col_target >= 2) { // 不處理第三攔以後
152+
continue;
153+
}
154+
155+
$cell_source = $data_source[$row_target][$col_target];
156+
157+
$data_target[$row_target][$col_target] = $cell_source;
158+
}
159+
}
160+
161+
$this->_Csv['final'] = (new FileCsvWriter)
162+
->setFilePath(__DIR__ . '/final.csv')
163+
;
164+
165+
$this->_Csv['final']
166+
->setData($data_target)
167+
->save()
168+
;
169+
170+
foreach ($this->_Csv as $csv) {
171+
$csv->close();
172+
}
173+
174+
}
175+
176+
}
177+
178+
179+
(new AppMerge)
180+
->run()
181+
;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
./merge.php
4+
5+
localc --headless --convert-to ods final.csv
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
localc --headless --convert-to csv:"Text - txt - csv (StarCalc)":44,34,76,,,,true 1.ods
4+
5+
localc --headless --convert-to csv:"Text - txt - csv (StarCalc)":44,34,76,,,,true,,,true 3.ods

0 commit comments

Comments
 (0)