Skip to content

Commit 15c7dfe

Browse files
committed
Add a new Google Sheet loader
1 parent 4d82a90 commit 15c7dfe

File tree

34 files changed

+3256
-68
lines changed

34 files changed

+3256
-68
lines changed

bin/docs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function execute(InputInterface $input, OutputInterface $output) : int
4040
__DIR__ . '/../src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/functions.php',
4141
__DIR__ . '/../src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/functions.php',
4242
__DIR__ . '/../src/adapter/etl-adapter-elasticsearch/src/Flow/ETL/Adapter/Elasticsearch/functions.php',
43-
__DIR__ . '/../src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/functions.php',
43+
__DIR__ . '/../src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/DSL/functions.php',
4444
__DIR__ . '/../src/adapter/etl-adapter-json/src/Flow/ETL/Adapter/JSON/functions.php',
4545
__DIR__ . '/../src/adapter/etl-adapter-meilisearch/src/Flow/ETL/Adapter/Meilisearch/functions.php',
4646
__DIR__ . '/../src/adapter/etl-adapter-parquet/src/Flow/ETL/Adapter/Parquet/functions.php',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/functions.php",
138138
"src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/functions.php",
139139
"src/adapter/etl-adapter-elasticsearch/src/Flow/ETL/Adapter/Elasticsearch/functions.php",
140-
"src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/functions.php",
140+
"src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/DSL/functions.php",
141141
"src/adapter/etl-adapter-json/src/Flow/ETL/Adapter/JSON/functions.php",
142142
"src/adapter/etl-adapter-meilisearch/src/Flow/ETL/Adapter/Meilisearch/functions.php",
143143
"src/adapter/etl-adapter-parquet/src/Flow/ETL/Adapter/Parquet/functions.php",

documentation/components/adapters/google-sheet.md

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,68 @@ composer require flow-php/etl-adapter-google-sheet:~--FLOW_PHP_VERSION--
2727
```php
2828
<?php
2929

30+
use function Flow\ETL\Adapter\GoogleSheet\{from_google_sheet};
3031
use Flow\ETL\Adapter\GoogleSheet\GoogleSheetRange;
3132
use Flow\ETL\DSL\GoogleSheet;
3233
use Flow\ETL\Flow;
3334

3435
$rows = (new Flow())
35-
->read(GoogleSheet::from($auth_config, $spreadsheet_document_id, $sheet_name)))
36+
->read(from_google_sheet($auth_config, $spreadsheet_document_id, $sheet_name)))
3637
->fetch();
3738
```
3839

39-
## Needed parameters
40+
## Required parameters
4041

4142
- `$auth_config`
4243

43-
- Create project: [console.cloud.google.com](https://console.cloud.google.com/projectcreate) choosing the right organization.
44-
- Enable google sheet API for created project on [api sheets.googleapis.com](https://console.cloud.google.com/apis/library/sheets.googleapis.com)
45-
- To work with google sheet enable it on [serviceaccounts](https://console.cloud.google.com/iam-admin/serviceaccounts/create) this will generate email for example `[email protected]`
46-
- Generate json (auth config) for created serviceaccounts on `Keys` tab.
44+
- Create a project: [console.cloud.google.com](https://console.cloud.google.com/projectcreate) choosing the right organization,
45+
- Enable Google Sheet API for a created project on [API sheets.googleapis.com](https://console.cloud.google.com/apis/library/sheets.googleapis.com),
46+
- To work with Google Sheet enable it on a [serviceaccounts](https://console.cloud.google.com/iam-admin/serviceaccounts/create) this will generate email for example `[email protected]`,
47+
- Generate JSON (auth config), for created "serviceaccounts" on `Keys` tab.
4748

4849
- `$spreadsheet_document_id` ID needs to be readded from the document we want to use, example URL `https://docs.google.com/spreadsheets/d/xyzID-for-documentxyz/edit` ID is `xyzID-for-documentxyz`
49-
- `$sheet_name` - Name of sheet from document you want to read.
50+
- `$sheet_name` - Name of the sheet from the document you want to read.
51+
52+
## Loader
53+
54+
```php
55+
<?php
56+
57+
use function Flow\ETL\Adapter\GoogleSheet\{google_create_spreadsheet,
58+
google_sheets,
59+
to_google_sheet};
60+
use function Flow\ETL\DSL\{config, flow_context};
61+
use Flow\ETL\Flow;
62+
use Flow\ETL\Row;
63+
use Flow\ETL\Rows;
64+
use Google\Service\Sheets;
65+
66+
// Create a Google spreadsheet
67+
$service = google_sheets(
68+
$auth_config,
69+
// Scope required to creating & updating spreadsheets
70+
Sheets::SPREADSHEETS
71+
);
72+
73+
$sheetName = 'Flow test sheet';
74+
$spreadsheetId = google_create_spreadsheet(
75+
$service,
76+
'Flow test spreadsheet',
77+
$sheetName,
78+
);
79+
80+
(new Flow())
81+
->process(
82+
new Rows(
83+
...\array_map(
84+
fn (int $i) : Row => Row::create(
85+
new Row\Entry\IntegerEntry('id', $i),
86+
new Row\Entry\StringEntry('name', 'name_' . $i)
87+
),
88+
\range(0, 10)
89+
)
90+
)
91+
)
92+
->write(to_google_sheet($service, $spreadsheetId, $sheetName))
93+
->run();
94+
```

documentation/upgrading.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ Please follow the instructions for your specific version to ensure a smooth upgr
55

66
---
77

8+
## Upgrading from 0.15.x to 0.16.x
9+
10+
### 1) Moved `src/Flow/ETL/Adapter/GoogleSheet/functions.php` file
11+
12+
The file was moved to subfolder and now is located at: `src/Flow/ETL/Adapter/GoogleSheet/DSL/functions.php`
13+
14+
---
15+
816
## Upgrading from 0.14.x to 0.15.x
917

10-
### 1) Removed `Flow\ETL\Row\Schema\Matcher` and implementations
11-
Schema Matcher was the initial attempt to implement a schema evolution next to schema validation that over
12-
time got replaced with different implementation of Schema Validator.
18+
### 1) Removed `Flow\ETL\Row\Schema\Matcher` and implementations
19+
Schema Matcher was the initial attempt to implement a schema evolution next to schema validation that over
20+
time got replaced with different implementation of Schema Validator.
1321

14-
### 2) Renamed `Flow\ETL\Row\Schema` namespace into `Flow\ETL\Schema`.
22+
### 2) Renamed `Flow\ETL\Row\Schema` namespace into `Flow\ETL\Schema`.
1523
This means all classes related to Schema now live under `Flow\ETL\Schema` namespace.
1624

1725
---

examples/topics/data_writing/elasticsearch/code.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require __DIR__ . '/vendor/autoload.php';
1010

1111
if (!\file_exists(__DIR__ . '/.env')) {
12-
print 'Example skipped. Please create .env file with Azure Storage Account credentials.' . PHP_EOL;
12+
print 'Example skipped. Please create .env file with Elasticsearch credentials.' . PHP_EOL;
1313

1414
return;
1515
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
auth.json.dist
2+
vendor
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use function Flow\ETL\Adapter\GoogleSheet\{from_google_sheet, google_create_spreadsheet, google_sheets, to_google_sheet};
6+
use function Flow\ETL\DSL\{data_frame, from_array, to_stream};
7+
use Google\Service\Sheets;
8+
9+
require __DIR__ . '/vendor/autoload.php';
10+
11+
if (!\file_exists(__DIR__ . '/auth.json.dist')) {
12+
print 'Example skipped. Please create .env file with Google Auth credentials.' . PHP_EOL;
13+
14+
return;
15+
}
16+
17+
$service = google_sheets(
18+
\json_decode((string) \file_get_contents(__DIR__ . '/auth.json.dist'), true, 512, JSON_THROW_ON_ERROR),
19+
Sheets::SPREADSHEETS
20+
);
21+
22+
$spreadsheetId = google_create_spreadsheet(
23+
$service,
24+
'Flow test spreadsheet',
25+
$sheetName = 'Flow test sheet',
26+
);
27+
28+
data_frame()
29+
->read(from_array([
30+
['id' => 1, 'text' => 'lorem ipsum'],
31+
['id' => 2, 'text' => 'lorem ipsum'],
32+
['id' => 3, 'text' => 'lorem ipsum'],
33+
['id' => 4, 'text' => 'lorem ipsum'],
34+
['id' => 5, 'text' => 'lorem ipsum'],
35+
['id' => 6, 'text' => 'lorem ipsum'],
36+
]))
37+
->write(to_google_sheet($service, $spreadsheetId, $sheetName))
38+
->run();
39+
40+
data_frame()
41+
->read(from_google_sheet($service, $spreadsheetId, $sheetName))
42+
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
43+
->run();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "flow-php/examples",
3+
"description": "Flow PHP - Examples",
4+
"license": "MIT",
5+
"type": "library",
6+
"require": {
7+
"flow-php/etl": "1.x-dev",
8+
"flow-php/etl-adapter-google-sheet": "1.x-dev"
9+
},
10+
"config": {
11+
"allow-plugins": {
12+
"php-http/discovery": false
13+
}
14+
},
15+
"archive": {
16+
"exclude": [
17+
"auth.json.dist",
18+
"vendor"
19+
]
20+
}
21+
}

0 commit comments

Comments
 (0)