lozemc/simple-google-sheets

使用PHP管理Google Sheets数据的简单方法

1.1.1 2024-07-11 14:40 UTC

This package is auto-updated.

Last update: 2024-09-11 15:03:46 UTC


README

Google Sheets

GoogleSheet

GoogleSheet是标准google/apiclient包的方便包装,为与Google Sheets交互提供易于使用的接口。

安装

使用Composer安装包

composer require lozemc/simple-google-sheets

用法

创建实例

use Lozemc\GoogleSheet;

// Replace 'your_table_id' and 'path/to/your/credentials-config.json' with actual values

$table_id = 'your_table_id';
$credentials = 'path/to/your/credentials-config.json';

$table = new GoogleSheet($table_id, $credentials);

设置工作表

$table->set_sheet('YourSheetName');

获取行

// Get all rows
$rows = $table->get_rows();

print_r($rows);

// [
//    [ 'A1 value', 'B1 value', 'C1 value' ],
//    [ 'A2 value', 'B2 value', 'C2 value' ],
//    [ 'A3 value', 'B3 value', 'C3 value' ]
// ]


// Get rows from a specific range
$range = 'B2:C3';
$rows = $table->get_rows($range);

print_r($rows);

// [
//    [ 'B2 value', 'C2 value' ],
//    [ 'B3 value', 'C3 value' ]
// ]

更新行

$table->update([['A1 value', 'B1 value']]);

// 

$range = 'A4';
$table->update([['A4 value', 'B4 value']], $range);

// 

$table->update([
    ['A1 value', 'B1 value'],
    ['A2 value', 'B2 value', 'C3 value'],
    ['A3 value', 'B3 value', '', 'D4 value'],
]);

追加行

$table->append([['Value 1', 'Value 2']]);

//

$table->append([['Value 1', 'Value 2']], 'A10');

简单示例

require_once __DIR__ . '/vendor/autoload.php';

use Lozemc\GoogleSheet;

$table_id = '1gncMRlsonFj2YzYw79QnfVA4Go5UcYkmfgG1T7Vb5Q';
$credentials = __DIR__ . '/credentials-config.json'; 

$table = new GoogleSheet($table_id, $credentials);


// Set the sheet name if not provided during initialization
$table->set_sheet('Sheet1');


// Append new rows
$table->append([
    ['Lisa', 'Anderson'],
    ['Jane', 'Jones'],
]);


// Get all rows
$rows = $table->get_rows();

// Display the retrieved rows
print_r($rows);


// Get rows from a specific range in another sheet
$rows = $table->set_sheet('Sheet2')->get_rows('B10:C20');

print_r($rows);


// Update data in a specific range
$table->update([['New value']], 'B10');
$row = $table->get_rows('B10');

print_r($row);

要求

  • PHP >=7.4.33

依赖项

许可

此包根据MIT许可授权。