Skip to content

Commit bdda26c

Browse files
committed
Add a new Google Sheet loader
1 parent 5f7b332 commit bdda26c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5862
-65
lines changed

bin/docs.php

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

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
"src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/functions.php",
140140
"src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/functions.php",
141141
"src/adapter/etl-adapter-elasticsearch/src/Flow/ETL/Adapter/Elasticsearch/functions.php",
142-
"src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/functions.php",
142+
"src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/DSL/functions.php",
143143
"src/adapter/etl-adapter-json/src/Flow/ETL/Adapter/JSON/functions.php",
144144
"src/adapter/etl-adapter-meilisearch/src/Flow/ETL/Adapter/Meilisearch/functions.php",
145145
"src/adapter/etl-adapter-parquet/src/Flow/ETL/Adapter/Parquet/functions.php",
@@ -208,6 +208,7 @@
208208
},
209209
"extra": {
210210
"google/apiclient-services": [
211+
"Drive",
211212
"Sheets"
212213
]
213214
},

documentation/components/adapters/google-sheet.md

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,69 @@ 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+
$spreadsheet = google_create_spreadsheet(
75+
$service,
76+
'Flow test spreadsheet',
77+
$sheetName,
78+
79+
);
80+
81+
(new Flow())
82+
->process(
83+
new Rows(
84+
...\array_map(
85+
fn (int $i) : Row => Row::create(
86+
new Row\Entry\IntegerEntry('id', $i),
87+
new Row\Entry\StringEntry('name', 'name_' . $i)
88+
),
89+
\range(0, 10)
90+
)
91+
)
92+
)
93+
->write(to_google_sheet($service, $spreadsheet->spreadsheetId, $sheetName))
94+
->run();
95+
```

documentation/upgrading.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Selected transformers were deprecated in favor of using `DataFrame::renameEach()
5858
- `RenameAllCaseTransformer` -> `RenameCaseTransformer`,
5959
- `RenameStrReplaceAllEntriesTransformer` -> `RenameReplaceStrategy`,
6060

61+
### 3) Moved `src/Flow/ETL/Adapter/GoogleSheet/functions.php` file
62+
63+
The file was moved to subfolder and now is located at: `src/Flow/ETL/Adapter/GoogleSheet/DSL/functions.php`
64+
6165
---
6266

6367
## Upgrading from 0.14.x to 0.15.x
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GOOGLE_SPREADSHEET_NAME='Flow test spreadsheet'
2+
GOOGLE_SHEET_NAME='Flow test sheet'
3+
GOOGLE_SHEET_EMAIL='[email protected]'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
auth.json
3+
vendor
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "flow-test",
4+
"client_email": "[email protected]",
5+
"client_id": "1234567890",
6+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
7+
"token_uri": "https://oauth2.googleapis.com/token",
8+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
9+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/flow-test%flow-test.iam.gserviceaccount.com",
10+
"universe_domain": "googleapis.com"
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
use Symfony\Component\Dotenv\Dotenv;
9+
10+
require __DIR__ . '/vendor/autoload.php';
11+
12+
if (!\file_exists(__DIR__ . '/auth.json')) {
13+
print 'Example skipped. Please create .env file with Google Auth credentials.' . PHP_EOL;
14+
15+
return;
16+
}
17+
18+
if (!\file_exists(__DIR__ . '/.env')) {
19+
print 'Example skipped. Please create .env file with Google Sheet details.' . PHP_EOL;
20+
21+
return;
22+
}
23+
24+
$dotenv = new Dotenv();
25+
$dotenv->load(__DIR__ . '/.env');
26+
27+
$service = google_sheets(
28+
\json_decode((string) \file_get_contents(__DIR__ . '/auth.json'), true, 512, JSON_THROW_ON_ERROR),
29+
Sheets::SPREADSHEETS
30+
);
31+
32+
$spreadsheet = google_create_spreadsheet(
33+
$service,
34+
$_ENV['GOOGLE_SPREADSHEET_NAME'],
35+
$sheetName = $_ENV['GOOGLE_SHEET_EMAIL'],
36+
$_ENV['GOOGLE_SHEET_EMAIL']
37+
);
38+
39+
data_frame()
40+
->read(from_array([
41+
['id' => 1, 'text' => 'lorem ipsum'],
42+
['id' => 2, 'text' => 'lorem ipsum'],
43+
['id' => 3, 'text' => 'lorem ipsum'],
44+
['id' => 4, 'text' => 'lorem ipsum'],
45+
['id' => 5, 'text' => 'lorem ipsum'],
46+
['id' => 6, 'text' => 'lorem ipsum'],
47+
]))
48+
->write(to_google_sheet($service, $spreadsheet->spreadsheetId, $sheetName))
49+
->run();
50+
51+
data_frame()
52+
->read(from_google_sheet($service, $spreadsheet->spreadsheetId, $sheetName))
53+
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
54+
->run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
"symfony/dotenv": "^7.2"
10+
},
11+
"config": {
12+
"allow-plugins": {
13+
"php-http/discovery": false
14+
}
15+
},
16+
"archive": {
17+
"exclude": [
18+
".env",
19+
"auth.json.dist",
20+
"vendor"
21+
]
22+
}
23+
}

0 commit comments

Comments
 (0)