PHP code example of goletter / hyperf-docs
1. Go to this page and download the library: Download goletter/hyperf-docs library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
goletter / hyperf-docs example snippets
return [
'default' => env('DOCS_PLATFORM', 'google'),
// 自定义平台 class 映射(工厂可扩展 / 覆盖)
'platforms_map' => [
// 'my_docs' => \App\Docs\Platform\MyDocsPlatform::class,
],
'platforms' => [
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect_uri' => env('GOOGLE_REDIRECT_URI'),
'api_key' => env('GOOGLE_API_KEY'),
'drive_root_folder' => env('GOOGLE_DRIVE_ROOT_FOLDER_NAME', 'Goletter'),
'scopes' => [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets',
],
],
'tencent' => [
'client_id' => env('TENCENT_DOCS_CLIENT_ID'),
'client_secret' => env('TENCENT_DOCS_CLIENT_SECRET'),
'redirect_uri' => env('TENCENT_DOCS_REDIRECT_URI'),
'drive_root_folder' => env('TENCENT_DOCS_DRIVE_ROOT_FOLDER_NAME', 'Goletter'),
// OAuth 文档要求 scope 固定为 all;实际权限以开放平台已开通权限为准
'scopes' => env('TENCENT_DOCS_SCOPES', 'all'),
],
],
];
use Goletter\Docs\DocsManager;
use Goletter\Docs\Contract\PlatformFactoryInterface;
use Hyperf\Di\Annotation\Inject;
#[Inject]
protected DocsManager $docs;
#[Inject]
protected PlatformFactoryInterface $factory;
$google = $this->docs->platform('google');
$tencent = $this->docs->platform('tencent');
$authUrl = $this->docs->auth('google')->getAuthUrl();
$file = $this->docs->sheets('google')->createSpreadsheet(
['access_token' => $googleToken['access_token']],
'报表'
);
$file = $this->docs->sheets('tencent')->createSpreadsheet(
[
'access_token' => $tencentToken['access_token'],
'open_id' => $tencentToken['open_id'],
],
'报表'
);
// config/autoload/docs.php
'platforms_map' => [
'my_docs' => \App\Docs\Platform\MyDocsPlatform::class,
],
// 或运行时注册
$this->factory->register('my_docs', MyDocsPlatform::class);
$this->factory->make('my_docs');
['id' => '...', 'title' => '...', 'url' => '...', 'raw' => /* 原始对象/数组 */]
use Goletter\Docs\DocsManager;
use Goletter\Docs\Platform\GooglePlatform;
/** @var GooglePlatform $platform */
$platform = $this->docs->platform('google');
$sheets = $platform->sheets();
$token = ['access_token' => $accessToken];
// 过期时:
// $token = $platform->refreshToken($refreshToken);
$spreadsheetId = '1f6KLBbJnsOMKMKUxkwH-Bfwm4YWkFA_RBibJbbLIq8M';
$gid = 649506568; // URL 里 #gid=
$title = $platform->getSheetTitleByGid($token, $spreadsheetId, $gid);
$range = $platform->rangeForGid($token, $spreadsheetId, $gid, 'A1:Z100');
// 只返回有内容的行,并裁掉行尾空单元格
// 第 4 个参数:每行至少几个非空单元格才保留(默认 1)
$rows = $sheets->readCells($token, $spreadsheetId, "gid:{$gid}");
$rows = $sheets->readCells($token, $spreadsheetId, "gid:{$gid}", 3);
// 按 F 列(也可用 0-based 下标,A=0)匹配
$matches = $sheets->findRows($token, $spreadsheetId, "gid:{$gid}", 'F', '333333333');
/*
[
[
'row' => 12,
'range' => "'Sheet'!A12:Z12",
'values' => [..., '333333333', true],
],
]
*/
$one = $matches[0] ?? null;
// null = 跳过该格(不会把保护列放进请求 range)
// '' = 清空(仍需编辑权限)
// true/false = 勾选框
$sheets->writeCells($token, $spreadsheetId, "gid:{$gid}!A1", [
[null, null, null, null, '名称', 'ID', null, true],
]);
// 更稳妥:直接写可编辑列
$sheets->writeCells($token, $spreadsheetId, "gid:{$gid}!F1", [['333333333']]);
$sheets->writeCells($token, $spreadsheetId, "gid:{$gid}!H1", [[true]]);
// 按行号编辑
$result = $sheets->updateRow(
$token,
$spreadsheetId,
"gid:{$gid}",
[null, null, null, null, '新名称', '333333333', null, true],
row: 12,
);
// 按列内容定位第一行再编辑(找不到返回 null)
$result = $sheets->updateRow(
$token,
$spreadsheetId,
"gid:{$gid}",
[null, null, null, null, '新名称', '333333333', null, false],
column: 'F',
match: '333333333',
);
/*
[
'row' => 12,
'range' => "'Sheet'!A12:Z12",
'values' => [..., '333333333', false], // 单行一维
]
*/
// 也可以 findRows + writeCells
$hit = $sheets->findRows($token, $spreadsheetId, "gid:{$gid}", 'F', '333333333')[0] ?? null;
if ($hit) {
$sheets->writeCells($token, $spreadsheetId, "gid:{$gid}!A{$hit['row']}", [
[null, null, null, null, '新名称', '333333333', null, true],
]);
}
// 按行号删除
$result = $sheets->deleteRow($token, $spreadsheetId, "gid:{$gid}", row: 12);
// ['row' => 12]
// 按列内容定位第一行再删除(找不到返回 null)
$result = $sheets->deleteRow(
$token,
$spreadsheetId,
"gid:{$gid}",
column: 'F',
match: '333333333',
);
// 单行(一维)
$result = $sheets->appendCells($token, $spreadsheetId, "gid:{$gid}", [
null, null, null, null, '名称', '333333333', null, true,
]);
/*
[
'row' => 12,
'range' => "'Sheet'!A12:Z12",
'values' => [..., '333333333', true], // 单行返回一维
]
*/
// 多行(二维);values 仍为二维
$result = $sheets->appendCells($token, $spreadsheetId, "gid:{$gid}", [
[null, null, null, null, '名称1', '111', null, true],
[null, null, null, null, '名称2', '222', null, true],
]);
$file = $sheets->createSpreadsheet($token, '报表');
$sheets->batchWrite($token, $spreadsheetId, [
['range' => "gid:{$gid}!A1", 'values' => [['a', 'b']]],
]);
$sheets->addSheet($token, $spreadsheetId, 'Sheet2');
$sheets->moveSpreadsheetToDateFolder($token, $spreadsheetId);
$sheets->shareSpreadsheetForAnyoneReader($token, $spreadsheetId);
use Goletter\Docs\Tencent\TencentAuth;
use Goletter\Docs\Tencent\TencentSheets;
use Hyperf\Di\Annotation\Inject;
#[Inject]
protected TencentAuth $auth;
#[Inject]
protected TencentSheets $sheets;
$authUrl = $this->auth->getAuthUrl(state: 'xyz'); // scope 固定为 all
$token = $this->auth->fetchToken($code);
// $token['access_token']、$token['refresh_token']、$token['open_id']
$file = $this->sheets->createSpreadsheet($accessToken, $openId, '报表');
$spreadsheetId = $file['ID'] ?? $file['id'];
// 读:spreadsheet v3(需 scope.sheet / scope.sheet.readonly)
$values = $this->sheets->readCells($accessToken, $openId, $spreadsheetId, 'A1:Z1000');
// 写:sheetbook v2
$this->sheets->writeCells($accessToken, $openId, $spreadsheetId, 'A1', [['姓名', '部门']]);
// 追加 / 按列查找(门面 sheets('tencent') 同样支持)
$sheets = $this->docs->sheets('tencent');
$tokenPayload = [
'access_token' => $accessToken,
'open_id' => $openId,
];
$sheets->appendCells($tokenPayload, $spreadsheetId, 'A1', [
'张三', '技术部',
]);
$sheets->findRows($tokenPayload, $spreadsheetId, 'A1:Z1000', 'A', '张三');
$sheets->updateRow($tokenPayload, $spreadsheetId, 'A1:Z1000', ['李四', '产品部'], column: 'A', match: '张三');
$sheets->deleteRow($tokenPayload, $spreadsheetId, 'A1:Z1000', column: 'A', match: '李四');
$this->sheets->moveSpreadsheetToDateFolder($accessToken, $openId, $spreadsheetId);
$this->sheets->shareSpreadsheetForAnyoneReader($accessToken, $openId, $spreadsheetId);
bash
php bin/hyperf.php vendor:publish goletter/hyperf-docs
bash
# Google
php bin/hyperf.php docs:test --platform=google --auth-url
php bin/hyperf.php docs:test --platform=google --token=xxx
# 腾讯文档
php bin/hyperf.php docs:test --platform=tencent --auth-url
php bin/hyperf.php docs:test --platform=tencent --token=xxx --open-id=yyy