taeluf / spreadsheet-converter
将.ods文件转换为.csv、.json、.sql,并创建一个.sqlite数据库
v0.1.x-dev
2021-09-11 05:05 UTC
Requires
- league/csv: 9.x-dev
- taeluf/json-to-mysql: v1.0.x-dev
Requires (Dev)
- taeluf/code-scrawl: v0.3.x-dev
This package is auto-updated.
Last update: 2024-09-22 07:57:03 UTC
README
电子表格转数据库(及其他转换)
将.ods文件转换为.csv、.json、.sql,并创建一个.sqlite数据库
安装
composer require taeluf/spreadsheet-converter v0.1.x-dev
或者在您的composer.json
文件中
{"require":{ "taeluf/spreadsheet-converter": "v0.1.x-dev"}}
依赖关系
- libreoffice(我们使用
libreoffice
cli命令转换.ods文件)
用法
查看test/example.php
和test/data
以获取一个工作示例。您可以通过执行test/example.php
来确保它在您的系统上正常工作。
- 创建一个类似于这样的php文件
chmod ug+x the-file.php
使其可执行。./the-file.php
执行它并构建您的数据库
#!/usr/bin/env php
<?php
$root_dir = dirname(__DIR__);
require($root_dir.'/vendor/autoload.php');
// .ods files are NEVER overwritten (except in case of error, maybe? Do use version control)
\Tlf\build_csv_conversions(
// you could do this:
// json_decode(file_get_contents(__DIR__.'/settings.json'));
// or a for loop over a scandir() or an array of settings files & run many separate conversions
[
// all settings shown are the defaults
'source_dir' => $root_dir.'/test/data/src/',
'path_prefix'=>$root_dir.'/test/data/raw-',
// bugfix: Fileformat encoding leaves some characters incompatible with utf8, so apply a fix with libreoffice cli
'use_filetype_conversion'=>true,
// WARNING: OVERWRITES FILES if true
// none of the files should be edited, generally. They're meant to be built from .ods spreadsheets
// Good to set them false if testing some kind of error
//
'csv'=>true,
'json'=>true,
'sql'=>true,
'sqlite'=>true,
]
);
文件结构
根目录可以是任何东西,我在此示例中使用data
data/
src/ -- all the data that you enter manually
covid-cases.ods
local-use-of-force-incidents.ods
local-traffic-stop-disparities.ods
delicious-gummi-bears.ods
raw-csv/
local_use_of_force_incidents.csv
-- and the other files
-- these are generated from the .ods files
raw-json/
-- same
raw-sql/
-- same
data.sqlite -- is overwritten on build & tables are named after their source files
-- so delicious_gummi_bears is a table (hyphens replaced with underscore)