ahmadpriatama / xls-woles
此包的最新版本(v0.1.1)没有提供许可证信息。
v0.1.1
2016-08-28 21:37 UTC
Requires
- phpoffice/phpexcel: ^1.8
Requires (Dev)
- phpunit/phpunit: ^5.5
This package is not auto-updated.
Last update: 2024-09-14 18:53:14 UTC
README
安装
安装此扩展的首选方式是通过composer。
安装
运行以下命令之一:
$ php composer.phar require ahmadpriatama/xls-woles "*"
或添加以下内容到您的composer.json
文件的require
部分。
"ahmadpriatama/xls-woles": "*"
to the
演示
查看demo
文件夹
使用示例
我们有一个如下所示的Book1.xlsx
中的儿童书籍列表
并且您的上司希望导入清洁数据,并具有以下规格
- 书名必须存储为小写字符串
- 必须将作者姓名拆分为名和姓
- ISBN必须存储为格式XXX-X-XX-XXXXXX-X
- 语言必须存储为常量值,例如:
Book::LANG_EN = 1
或Book::LANG_NON_EN = 2
这是您的脚本
$config = json_decode(file_get_contents($fileConfig), true);
$data = \XLSWoles\Reader::load($fileInput)
->setSheetName('Sheet1')
->setColumnConfig($config['columns'])
->setDataRange('a2:e6')
->fetch();
column-config.json
{
"columns" : {
"book-name": {
"filters": [
"string-lowercase"
]
},
"author-name": {
"filters":[
"SplitNameFilter"
]
},
"publisher": {},
"isbn": {
"filters": [
"regex||-||",
"regex||(\\d{3})(\\d{1})(\\d{2})(\\d{6})(\\d{1})||$1-$2-$3-$4-$5"
]
},
"language": {
"filters": [
"string-lowercase",
"regex||english||1"
]
}
}
}
您可以看到author-name
字段有SplitNameFilter
,这是一个自定义过滤器,其源代码如下所示
<?php
/**
* Class SplitNameFilter
* @author Ahmad Priatama <ahmad.priatama@gmail.com>
* @since 2016.08.28
*/
class SplitNameFilter
{
/**
* @param string $input Input String
* @return array
*/
public function process($input)
{
$arr = explode(' ', $input);
$count = count($arr);
if ($count == 1) {
return [
'first-name' => $arr[0],
'last-name' => null,
];
} else {
return [
'first-name' => implode(' ', array_slice($arr, 0, $count-1)),
'last-name' => $arr[$count-1],
];
}
}
}
print_r($data);
应该给出
Array
(
[0] => Array
(
[book-name] => where the wild things are
[author-name] => Array
(
[first-name] => Maurice
[last-name] => Sendak
)
[publisher] => HarperCollins
[isbn] => 978-0-06-443178-1
[language] => 1
)
[1] => Array
(
[book-name] => the snowy day
[author-name] => Array
(
[first-name] => Ezra Jack
[last-name] => Keats
)
[publisher] => Puffin Books
[isbn] => 978-0-14-050182-7
[language] => 1
)
[2] => Array
(
[book-name] => goodnight moon
[author-name] => Array
(
[first-name] => Margaret Wise
[last-name] => Brown
)
[publisher] => HarperCollins
[isbn] => 978-0-06-443017-3
[language] => 1
)
[3] => Array
(
[book-name] => owl moon
[author-name] => Array
(
[first-name] => Jane
[last-name] => Yolen
)
[publisher] => Philomel Books
[isbn] => 978-0-39-921457-8
[language] => 1
)
[4] => Array
(
[book-name] => the giving tree
[author-name] => Array
(
[first-name] => Shel
[last-name] => Silverstein
)
[publisher] => Harper & Row
[isbn] => 978-0-06-025665-4
[language] => 1
)
)
内置过滤器
- 正则表达式替换
- 字符串小写
- 字符串大写
- 字符串转时间
- 默认值
- 空时为空值