patrikgrinsvall/laravel-import-spreadsheet

Laravel Artisan 命令,用于使用模型将 Google Docs 表格导入数据库

dev-master 2021-11-24 03:29 UTC

This package is auto-updated.

Last update: 2024-09-24 09:28:42 UTC


README

安装

composer require patrikgrinsvall/laravel-import-spreadsheet

使用示例

  1. 始终创建新记录,使用 'Post' 模型并不要与现有记录匹配,即使在数据库错误发生时也要继续,并且将所有值添加到名为 json_data 的 JSON 列中,而不缓存响应
    php artisan import:google-spreadsheet \
    -s <spreadsheetid> \
    --create-new \
    --model=App\\Models\\Post \
    --skip-errors \
    --json-column=json_data \
    --cache-ttl=0
  1. 始终创建新记录,使用 'Post' 模型并不要与现有记录匹配,将表格保存到 storage_path("mytmpcsv.csv")
php artisan import:google-spreadsheet  \
-s spreadsheetid \
--create-new \
--model=App\\Models\\Post \
--filename=mytmpcsv.csv \
  1. 使用 'database_column' 作为唯一键导入数据,这意味着所有行都将与 CSV 中的此对应值对应的数据库记录匹配,使用 'Post' 模型,将所有字段作为 JSON 保存在 'post_attributes' 列中,并且不缓存响应
php artisan import:google-spreadsheet -s spreadsheetid \
-u="Spreadsheet header column=datebase_column"  \
--model=App\\Models\\Post \
--json-column=post_attributes \
--cache-ttl=0

从命令行运行

php artisan import:google-spreadsheet

选项

  • -s --spreadsheet= : 表格的 ID,位于表格 URL 中 /d/ 后的字符序列。表格必须公开, - 必需
  • -m --model= : 要使用的模型。表格中的列必须与模型中的 snake_case 列匹配,否则将静默跳过 - 必需
  • -u --unique-key= : 例如 title=identifier,这将取 CSV 中的标题列并与模型中的 identifier 列匹配,该列必须是唯一的 - 可选
  • -j --json-column= : 如果模型有一个所有数据都应放入的 JSON 列,请在此处指定。 - 可选
  • -c --create-new : 总是创建新记录,永远不要匹配键并尝试更新 - 可选
  • -f --filename= : 使用此文件名作为临时存储 - 默认=import.csv
  • -d --cache-ttl= : 默认情况下,来自 Google Docs 的响应会缓存 3600 秒,此选项会更改此值,使用 0 来禁用缓存 - 可选
  • -e --skip-errors : 如果可能的话跳过错误并继续下一行,例如重复键或插入数据时发生错误 - 可选

配置

  • 如果经常运行更新并且仅针对一个表格 ID 使用,则运行 artisan vendor:publish 并设置此 ID 环境变量或配置文件是有意义的。

config/import-spreadsheet.php

 /*
    |--------------------------------------------------------------------------
    | Google Spreadsheet ID
    |--------------------------------------------------------------------------
    | This is the google spreadsheet id, it can be taken from url in browser.
    | If importing from multiple spreadsheets across serveral commands
    | dont set this in config
     */
    'spreadsheet' => env('IMPORT_SPREADSHEET_ID', null),
    /*
    |--------------------------------------------------------------------------
    | unique-key
    |--------------------------------------------------------------------------
    | The attribute in model or column in database to use for mapping
    | csv header column. Example:
    | 'csv_email_column=email'
    | will map `csv_email_column` column in spreadsheet against email
    | column in the table
     */
    'unique-key' => env('IMPORT_SPREADSHEET_UNIQUE_KEY', null),
    /*
    |--------------------------------------------------------------------------
    | Model
    |--------------------------------------------------------------------------
    | The FQCN to the model to use, example App\\Models\\User
     */
    'model' => env('IMPORT_SPREADSHEET_MODEL', null),
    /*
    |--------------------------------------------------------------------------
    | Json column
    |--------------------------------------------------------------------------
    | If all csv columns should be mapped into a json column in databse
    | table, this is the database column name
     */
    'json-column' => env('IMPORT_SPREADSHEET_JSON_COLUMN', null),
    /*
    |--------------------------------------------------------------------------
    | cache ttl
    |--------------------------------------------------------------------------
    | By default requests to spreadsheets are cached for an hour, 0 will disable
     */
    'cache-ttl' => env('IMPORT_SPREADSHEET_CACHE_TTL', 3600),
    /*
    |--------------------------------------------------------------------------
    | filename
    |--------------------------------------------------------------------------
    | If the original csv file needs to be processed further by another job
    | this is the filename the downloaded file will have on disk
     */
    'filename' => env('IMPORT_SPREADSHEET_FILENAME', storage_path('csvimport.csv'))

贡献

PR 非常受欢迎

兼容性

仅在 Laravel 8.0 和 8.1 上进行测试