rationalboss/gsheet-manager

Google表格客户端

v0.1.7 2017-03-06 07:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:35:43 UTC


README

PHP版本的Google表格客户端。此版本需要"google/apiclient"包。

开始使用

1. 获取密钥文件

  1. 登录Google开发者控制台
  2. 创建新项目
  3. 在项目中创建服务帐户凭证
  4. 将密钥文件下载为JSON格式

2. 创建表格

  1. Google Drive中创建一个新的表格
  2. 授权密钥文件中找到的“client_email”电子邮件地址,允许其读取和编辑。
  3. 从地址栏保存文件ID

3. 通过PHP访问

$client = Google_Spreadsheet::getClient("the/path/to/credential.json");
// Get the file by file ID
$file = $client->file("XXXxxxXXXXxxxXXXX");
// Get the sheet by title
$sheet = $file->sheet("Sheet1");
// Flush all rows in the sheet
var_dump($sheet->items);

用法

选择行

// Array
$items = $sheet->select(array("id" => "1"));
// Closure
$items = $sheet->select(function($row){
	return (int) $row["age"] < 30;
});

插入新行

$sheet->insert(array(
	"name" => "John",
	"age" => 23,
	"email" => "john@example.com"
));

更新列的值

$sheet->update(
	8, // row number
	"name", // field's name (or column number as Integer)
	"Tom"
);

$sheet->update(
	array(8,16,24), // row numbers
	"name",
	"Tom"
);

$sheet->update(
	array(
		"name" => "Tom" // condition to select
	),
	"email",
	"tom@example.com"
);

$sheet->update(
	function($row){
		return (int) $row["age"] > 80; // condition to select as closure
	},
	"active",
	"false"
);

获取最新的表格数据

$items = $sheet->fetch(true)->items;

保存缓存

$client->config(array(
	"cache" => true,
	"cache_dir" => "cache",
	"cache_expires" => 3600
));

要求