sectsect/google-spreadsheet-to-db 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sectsect/google-spreadsheet-to-db

Composer 安装命令:

composer require sectsect/google-spreadsheet-to-db

包简介

Pulls Google Spreadsheet data via Google’s API and saves it in your wordpress database.

README 文档

README

Plugin Check PHP Unit Tests PHPStan PHP Coding Standards Latest Stable Version

The "Google Spreadsheet to DB" plugin is designed for WordPress and facilitates the import of data from Google Sheets into a WordPress database using Google's Sheets API (v4). It supports data manipulation before saving and is configurable via a WordPress admin interface.

Features

  • Data Import: Pulls data from Google Sheets and saves it directly into the WordPress database.
  • Customization: Offers settings for defining constants, spreadsheet IDs, names, and configuring data formats.
  • Admin Interface: Provides an admin page for easy management and configuration of the plugin settings.

Requirements

  • PHP version 8.0 or higher.
  • Composer for managing PHP dependencies.

Get Started

1. Clone this Repo into your wp-content/plugins directory.
cd /path-to-your/wp-content/plugins/
git clone git@github.com:sectsect/google-spreadsheet-to-db.git
2. Remove vendor/ in .gitignore file.
cd google-spreadsheet-to-db
nano .gitignore
- vendor/
3. Install composer packages
cd functions/composer/
composer install
4. Activate the plugin through the 'Plugins' menu in WordPress.

Settings

Getting Your Spreadsheet Ready for Programmatic Access

By default, a new spreadsheet cannot be accessed via Google’s API. We’ll need to go to your Google APIs console and create a new project and set it up to expose your Spreadsheets’ data.

  1. Go to the Google APIs Console.
  2. Create a new project.
  3. Click Enable API. Search for and enable the Google Sheets API.
  4. Create credentials for a Web Server to access Application Data.
  5. Name the service account and grant it a Project Role of Editor.
  6. Download the JSON file.
  7. Copy the JSON file to your app directory and rename it to client_secret.json
  8. ⚠️ Set client_secret.json in a location to deny web access on your server.

We now have a big chunk of authentication information, including what Google calls a client_email, which uniquely represents this OAuth service account.
Grab the value of client_email from your client_secret.json, and head back to your spreadsheet. Click the Share button in the top right, and paste the client_email value into the field to give it edit rights.
Hit send. That’s it! 👌

  1. Set the define() constants for client_secret.json in wp-config.php.
define( 'GOOGLE_SS2DB_CLIENT_SECRET_PATH', '/path/to/your/client_secret.json' );
  1. Go to Settings -> Google Spreadsheet to DB on your WordPress Admin-Panel.
  2. Set the following values and save it once.
  • Data format to be stored in database
    • json_encode
    • json_encode (JSON_UNESCAPED_UNICODE)
  1. Click the Import from Google Spreadsheet button. 🎉
  • Spreadsheet ID
  • Spreadsheet name (Optional)
  • Single Sheet name
  • Top Header Row
  • Title (Optional)

Filters

Filtering the Array

You can edit the array got from Google API with add_filter( 'google_ss2db_before_save', $function_to_add ) in your functions.php before saving to database.

add_filter( 'google_ss2db_before_save', function ( $row, $worksheet_id, $worksheet_name, $sheet_name ) {
  // Example
  if ( $worksheet_name === 'My Spreadsheet' && $sheet_name === 'Sheet1' ) {
    // Do something.

    return $something;
  }

  return $row;
}, 10, 3 );

And also use add_filter('google_ss2db_after_save', $return_array ) to perform any processing with the return value.

add_filter( 'google_ss2db_after_save', function ( $data ) {
  if ( 'My Spreadsheet' === $data['worksheet_name'] ) {
    // $id              = $data['id'];
    // $date            = $data['date'];
    // $title           = $data['title'];
    // $value           = $data['value'];
    // $work_sheet_id   = $data['worksheet_id'];
    // $work_sheet_name = $data['worksheet_name'];
    // $sheet_name      = $data['sheet_name'];
    // $result          = $data['result']; // `int|false` The number of rows inserted, or false on error.

    // Example
    my_callback( $data );
  }
});

APIs

new Google_Spreadsheet_To_DB_Query();

Parameters

Parameter Type Notes Default Value
where array array()
relation string AND or OR AND
[array] array
key string id or date or worksheet_id or worksheet_name or sheet_name or title false
value int e.g. 3 / 2020-09-01 12:00:00 false
compare string e.g. = > < >= <= <> != =
orderby string id or date or worksheet_id or worksheet_name or sheet_name or title date
order string DESC or ASC DESC
limit int number of row to get All Data
📝 You can also use -1 to get all data.
offset int number of row to displace or pass over 0

Usage Example

Get all rows

$sheet = new Google_Spreadsheet_To_DB_Query();
$rows  = $sheet->getrow();
foreach ( $rows as $row ) {
  $id   = $row->id;
  $date = $row->date;
  $val  = json_decode( $row->value );
}

Get 3 rows from the 4th in ascending order by ID

$args = array(
  'orderby' => 'id',
  'order'   => 'ASC',
  'limit'   => 3,
  'offset'  => 3,
);
$sheet = new Google_Spreadsheet_To_DB_Query( $args );
$rows  = $sheet->getrow();
foreach ( $rows as $row ) {
  $id   = $row->id;
  $date = $row->date;
  $val  = json_decode( $row->value );
}

Get the row with specific ID

$args = array(
  'where' => array(
    array(
      'key'   => 'id',
      'value' => 3,
    )
  ),
);

Get 3 rows with specific Worksheet ordered by ID

$args = array(
  'orderby' => 'id',
  'order'   => 'ASC',
  'limit'   => 3,
  'where'   => array(
    array(
      'key'     => 'worksheet_name',
      'value'   => 'My Spreadsheet',
      'compare' => '='
    ),
  ),
);

Get the rows larger than or equal the specified datetime

$args = array(
  'where' => array(
    array(
      'key'     => 'date',
      'value'   => '2020-08-01 12:34:56',
      'compare' => '>=',
    )
  ),
);

Get the rows with multiple conditions

$args = array(
  'orderby' => 'id',
  'order'   => 'DESC',
  'limit'   => 10,
  'offset'  => 10,
  'where'   => array(
    'relation' => 'AND', // or 'OR'
    array(
      'key'     => 'date',
      'value'   => '2020-08-01 12:34:56',
      'compare' => '>='
    ),
    array(
      'key'     => 'worksheet_name',
      'value'   => 'My Spreadsheet',
      'compare' => '='
    ),
  ),
);

Notes

  • Tested on WordPress v6.3.1

For Developers

  • This plugin saves Spreadsheet's data to the global area, not to each post. If you want to have Spredsheet data for individual posts, you can link data ID with custom fields.

  • The data is added and stored in the wp_google_ss2db table as a JSON-encoded array.

    id date worksheet_id worksheet_name sheet_name title value
    1 2021-08-27 00:00:00 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms My Spreadsheet Sheet1 Data-01 {"area":{"a":["brooklyn","bronx","Queens","Manhattan"],"b":["brooklyn","bronx","Queens","Manhattan"]}}
  • This Plugin does not hosting on the wordpress.org repo in order to prevent a flood of support requests from wide audience.

Debug Mode

To enable debug mode, add the following constant to your wp-config.php:

define( 'GOOGLE_SS2DB_DEBUG', true );

When debug mode is enabled, importing a spreadsheet will return a detailed JSON response instead of the usual redirect. The response includes the following information:

  • result: A boolean indicating the success or failure of the process
  • data: Details of the saved data
  • post_data: Sanitized post data
  • referer: The redirect URL

Note: Always set GOOGLE_SS2DB_DEBUG to false or remove the constant in production environments.

Troubleshooting

This plugin depends on Guzzle v7, which may conflict with other WordPress plugins or Composer packages using Guzzle v6.
If you encounter errors like:

Uncaught Error: Call to undefined method GuzzleHttp\Utils::chooseHandler()

This is likely due to a Guzzle version conflict. To resolve:

  1. Update other plugins/packages to versions compatible with Guzzle v7
  2. Find alternative solutions that don't conflict with this plugin's dependencies

Change log

See CHANGELOG file.

License

See LICENSE file.

✌️

A little project by @sectsect

sectsect/google-spreadsheet-to-db 适用场景与选型建议

sectsect/google-spreadsheet-to-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「spreadsheet」 「plugin」 「wordpress」 「github」 「array」 「google」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sectsect/google-spreadsheet-to-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sectsect/google-spreadsheet-to-db 我们能提供哪些服务?
定制开发 / 二次开发

基于 sectsect/google-spreadsheet-to-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2017-07-23