mechastorm / google-spreadsheet-exporter
用于处理从Google电子表格提取数据并将其导出为其他文件格式的库
Requires
- php: >=5.4.0
- asimlqt/php-google-spreadsheet-client: 2.1.*
- google/apiclient: 1.0.*@beta
Requires (Dev)
- asimlqt/php-google-spreadsheet-client: 2.1.*
- google/apiclient: 1.0.*@beta
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-24 02:57:44 UTC
README
该库的主要功能集中在从Google电子表格读取数据并将其输出为可读的文件。主要用于本地化和复制管理,但也可用于任何需要将Google电子表格中的数据转换为其他文件格式的场景。
一个示例的电子表格,它被用来读取,可以在这里查看
这些库可以帮助
- 通过服务账户从Google电子表格中读取内容
- 转换这些数据。目前仅支持层次化的PHP数组格式
- 将数据写入文件格式。目前支持简单的PHP文件。示例输出可以在
examples/sample_ouput中查看
框架集成
该库本身是构建为与框架无关(没有框架依赖)。但有一些与现有框架示例的集成
- 在我制作的另一个包中,Laravel Language Google Spreadsheet Importer。
背景
目标是
- 避免硬编码复制文本
- 减少编辑复制文本所需的开发周期
- 优雅地处理复制文本的多个翻译
该功能最初在由@iskandar领导的Nudge Social Media中使用。这已被证明是一种非常快速且方便的方式,让非技术用户将一些数据写入他们熟悉的电子表格中,并让开发者以可读的格式提取这些数据。
原始库大量使用了Zend_Gdata来访问Google电子表格。Zend_Gdata不再维护,并且Google API正在迁移到新的OAuth。
我想主要使用Google电子表格进行复制管理和翻译,但不得不使用较新的Google API。
待办事项
- PhpUnit测试
- 文档
- 贡献
设置与安装
步骤1. 获取您的Google API凭据
访问https://console.developers.google.com/project并创建一个新的项目。
进入您的项目设置,点击左侧菜单,"APIs & AUTH" > "APIs"。启用"Drive API"。
然后点击左侧菜单,"APIs & AUTH" > "Credentials",并点击"Create new Client ID"。选择"Service Account"并生成您的客户端ID。
您的凭据可能看起来像这样
Client ID 11111111111-abcdef.apps.googleusercontent.com Email address 11111111111-abcdef@developer.gserviceaccount.com Public key fingerprints 1234567890
您还将被提示下载一个p12私钥证书(例如,1234567890-privatekey.p12)
步骤2. 授予电子表格访问权限
我们假设您已经设置好了电子表格,就像示例中那样
重要
在此阶段,客户端电子邮件地址应该是您用来共享电子表格的地址。
您可以授予它只读访问权限。未来的更新可能包括更新电子表格,届时您将需要提供编辑访问权限。
配置Composer
安装主要通过composer进行。
在您的项目中创建一个composer.json文件,并添加以下内容:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mechastorm/google-spreadsheet-exporter.git"
},
{
"type": "vcs",
"url": "https://github.com/asimlqt/php-google-spreadsheet-client"
}
],
"require": {
"mechastorm/google-spreadsheet-extractor": "dev-master"
}
}
使用示例
如果您没有使用PHP框架或不支持自动加载的框架,请确保包含composer的自动加载器。
此文件的示例可以在examples/basic.php中查看。让我们假设您正在使用此样本电子表格
require 'vendor/autoload.php'; use Mechastorm\Google\ApiHelper; use Mechastorm\Google\Spreadsheet\Data\Exporter; use Mechastorm\Google\Spreadsheet\Data\FormatWriters\LangPhpWriter; use Mechastorm\Google\Spreadsheet\Data\Transformers\LocalePhpArray; // Get the json encoded access token. $authResponse = ApiHelper::getAuthByServiceAccount( array( 'application_name' => 'name_of_application', 'client_id' => 'service_account_client_id', 'client_email' => 'service_account_client_email', 'key_file_location' => 'location-to-your-private-key-p12-file', // This is the location of the P12 private key file you had donwloaded ) ); $accessToken = $authResponse->access_token; echo 'Service Account Access Token: ' . $accessToken; // Connect to the Google Spreadsheet $gSSExporter = new Exporter(array( 'access_token' => $accessToken, 'spreadsheet_name' => 'google-spreadsheet-exporter Sample Spreadsheet' // It must match EXACTLY the name )); $gSSExporter->setWorksheets(); // Instantiate a transformer $transformer = new LocalePhpArray(array( 'locales' => array( 'en_GB', // Must match the columns on the spreadsheet 'fr_CA', 'my_MY', 'ja_JP', ), )); // Instantiate a write $outputFolder = 'sample_output'; $writer = new LangPhpWriter(array( 'path' => $outputFolder, )); // Process the worksheets and output them to the desired format $gSSExporter->processWorksheets( array( 'Web Copy', // It must match EXACTLY the name ), $transformer, $writer ); echo "Done - please check {$outputFolder} for the files\n";
同样,样本输出可以在examples/sample_output中查看。
测试
即将推出!
贡献者
- Shih Oon Liong (@mechastorm)
致谢
- Iskandar Najmuddin(@iskandar) 及 Matthew Long (@matthewongithub) 对启发此版本的原始Kohana库的开发做出了贡献
许可证
在Apache 2.0许可证下发布。
