xmlsquad / capture-lookups
抓取 Google Sheets 并将数据存储为 CSV 文件
v0.3.3
2018-10-15 06:44 UTC
Requires
- php: >=7.1
- google/apiclient: ^2.0
- symfony/console: ^3.4
- symfony/filesystem: ^3.4
- symfony/yaml: ^3.4
- xmlsquad/xml-authoring-library: 0.3.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.11
- phpunit/phpunit: ^7.1
This package is not auto-updated.
Last update: 2024-09-29 05:15:00 UTC
README
一个 Symfony Console 命令。搜索列出 Google Sheets URL 的配置文件,抓取 Sheets 并将其数据作为 CSV 文件本地存储。
设计用于 https://github.com/xmlsquad/xml-authoring-tools 的 Symfony Console 应用程序上下文,该应用程序反过来又用于基于 xml-authoring-project 的已知目录结构。
使用说明
指定要收集的查找表
我们假设此命令是在 xml-authoring-project 的上下文中运行的。即,目录结构的要点是已知的。
使用 mapping.yaml
配置文件,该文件定义了必须收集的 Google Sheets 的位置。
示例 mapping.yaml
LookupTableA: # (string) Specifies the URL of the sheet to look into url: "https://docs.google.com/spreadsheets/d/1jOfsClbTj15YUqE-X2Ai9cvyhP-GLvP8CGZPgD1TysI/edit#gid=0" # (int) Sets at what row number we'll start reading data - use if you want to skip the beginning of the sheet, for example a header startingFromRow: 2 # (bool) Enable or disable fetching data in a batch. Doing so is faster, but may fail if there is a lot of data to be fetched batchGet: true LookupTableB: url: "https://docs.google.com/spreadsheets/d/1jOfsClbTj15YUqE-X2Ai9cvyhP-GLvP8CGZPgD1TysI/edit#gid=0" startingFromRow: 2 batchGet: false
使用命令
- 检出仓库
- 使用
composer install
安装依赖项 - 在项目根目录或任何 PHP 可访问的父目录中放置
gApiServiceAccountCredentials.json
文件 - 运行
bin/capture-lookups
以查看所有可用映射 - 运行
bin/capture-lookups --sheet=LookupTableA
以交互式运行命令 - 运行
bin/capture-lookups --sheet=LookupTableA --no-interaction
以不带任何提示运行命令,跳过具有风险名称的文件或现有文件 - 运行
bin/capture-lookups --sheet=LookupTableA --no-interaction --force
以不带任何提示运行命令,**覆盖现有文件**并**使用清理后的文件名**
单元测试
- 安装依赖项
- 运行
./vendor/bin/phpunit
跳过的选项卡 - 命名约定
我说的 Google Sheets 选项卡 是指工作簿内的一个工作表。
任何以尾随下划线结尾的 Google Sheets 选项卡都将被考虑为跳过。
foo_
将被 跳过。foo
不会被 跳过。_foo
也不会 被跳过。
连接到 GSuite
Google Api 用于验证访问 GSuite 的文件应位于 xml-authoring-project 的根目录。
ping-drive 项目解释了如何设置以连接到 GSuite。
运行命令
当运行命令时,它将
- 在当前工作目录中搜索 XmlAuthoringProjectSettings.yaml,如果未找到,则递归地在父目录中查找,直到找到名为 XmlAuthoringProjectSettings.yaml 的文件。
- 确定写入的
DestinationDirectory
- 如果将
DestinationDirectory
选项传递给命令,则使用该选项。 - 如果没有将
DestinationDirectory
选项传递给命令,则将其设置为默认的DestinationDirectory
(见下文)。- 默认的
DestinationDirectory
是命令调用的工作目录。
- 默认的
- 如果将
- 对于配置文件中指定的每个查找表
- 转到 GSuite 上的 Google Sheet
- 确定并记录 Google Sheet 的名称
- 对于该工作表中的每个选项卡
- 如果选项卡的名称表明应该忽略(有尾随下划线),则忽略该选项卡,跳过并移动到下一个选项卡。
- 否则,记录选项卡名称
- 将Google表格的名称与标签名称组合起来,设置生成的CSV文件的名称:
<GoogleSheetName>-<TabName>.csv
。 - 检查名称以确保它仅由字母数字字符、点、连字符或下划线组成。(即,如果用作Windows或MacOS上的文件名,则不太可能导致问题)
- 如果名称包含无效字符,向STD_OUT和STD_ERR写入有意义的错误消息,并带错误代码退出。
- 检查是否已将具有该名称的CSV文件存储在目标目录中
- 如果它已经存在,并且没有设置
-f
(--force)标志,询问用户“允许覆盖文件 y/n?”。建议的默认提示为不,[n]
。 - 如果它已经存在,并且设置了-f (--force)标志,则无需提示用户即可覆盖现有文件。
- 否则,以所选名称创建CSV文件。
- 将Google表格标签的内容作为CSV文件写入。(逗号分隔,使用双引号封装字符串)
待办事项
- 使用
AbstractCommand
进行代码重用