aigorlaxy / google-sheets-model-importer
A simple trait to insert or update data to models from Google Sheets with just the link of the published spreadsheet. No need of Google API.
Installs: 2 459
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/database: ^8 || ^9 || ^10 || ^11 || ^12
- illuminate/support: ^8 || ^9 || ^10 || ^11 || ^12
README
A simple Laravel support class to insert or update data to models from Google Sheets with just the link of the published spreadsheet. No need of Google API.
Installation
You can install the package via composer:
composer require aigorlaxy/google-sheets-model-importer
Using
-
Create a Google Spreadsheet.
-
Publish it to internet and set general access to anyone with the link can be a viewer.
-
Include the trait in your model class.
-
$spreadsheetId: is the id of your google sheets. You will use your published Google Sheets link to get it. The link will look something like that: https://docs.google.com/spreadsheets/d/e/YOU_GOOGLE_SHEET_SPREADSHEET_ID/something_else
-
$sheetIds: If you have just one tab for that model, use the gid as a string. If you have more than one tab for the same model, use array. All models must match the same column schema to work. The link will look something like that: https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID/edit?gid=YOUR_SHEET_ID
-
$updateColumnIndex: Optionaly you can set a different primary key to check for updates. if your primary key is not default id or you want to track updates based on another column. If not set, it will assume that id column is your primary key.
-
$colunsToSkip: You can also optionaly set columns to be skipped. The app will search for columns that contains any of that strings and will skip the import for that ones. You can set a single string or an array.
-
Example of usage:
Use GoogleSheetsImportable. $model = User::class; $googleSpreadSheetId = '1gaLFuSnh20kggxEaasr511s15vt3olKqp9o12HenDLI3vA7pg'; $sheetId = '15144122'; $model::getFreshTableFromGoogleSheets($spreadsheetId, $sheetIds); // Truncate the current table and inserting the new data. $model::updateOrCreateFromGoogleSheets($spreadsheetId, $sheetIds); // Updating and inserting new data.
- Any issue or suggestions, please send me an e-mail: igor1523@gmail.com