Sheetsu PHP库可通过作曲家获得。
您可以编辑composer.json文件,也可以在终端中点击此命令
composer require emilianozublena/sheetsu-php
您需要实例化主床单对象,并给出SheetID,可以在Sheetsu仪表板上找到此URL。记住使用作曲家的自动加载。
require ( ' vendor/autoload.php ' );
use Sheetsu Sheetsu ;
$ sheetsu = new Sheetsu ([
' sheetId ' => ' sheetId '
]);如果您对API的HTTP基本身份验证打开,则应在此处传递key和secret ,例如:
require ( ' vendor/autoload.php ' );
use Sheetsu Sheetsu ;
$ sheetsu = new Sheetsu ([
' sheetId ' => ' sheetId ' ,
' key ' => ' key ' ,
' secret ' => ' secret '
]);如果需要,您可以在创建后重新初始化(或初始化)库
# Initialize after creation
$ sheetsu = new Sheetsu ();
$ sheetsu -> initialize ([
' sheetId ' => ' sheetId ' ,
' key ' => ' key ' ,
' secret ' => ' secret '
])Sheetsu PHP库带有集合抽象数据类型的少量实现。
模型是集合的单位(在这种情况下,每个模型代表给定表的一行)。
您可以做类似的事情,而不是将数组送给Sheetsu客户端(用于CRUD操作):
$ collection = new Collection ();
$ collection -> addMultiple ([
Model:: create ([ ' name ' => ' John ' ]),
Model:: create ([ ' name ' => ' Steve ' ])
]);
$ response = $ sheetsu -> create ( $ collection );收集和型号是您每次调用API时都将获得的两个对象。
链接到文档
要将数据添加到Google电子表格中,请发送一个数组,一系列数组,或者更简单地使用模型或集合;)
# Adds single row from array
$ sheetsu -> create ([ ' name ' => ' John ' ]);
# Adds multiple rows from array
$ sheetsu -> create ([
[ ' name ' => ' John ' ],
[ ' name ' => ' Steve ' ]
]);
# Adds single row from Model
$ sheetsu -> create (Model:: create ([ ' name ' => ' John ' ]));
# Adds multiple rows from Collection
$ collection = new Collection ();
$ collection -> addMultiple ([
Model:: create ([ ' name ' => ' John ' ]),
Model:: create ([ ' name ' => ' Steve ' ])
]);
$ response = $ sheetsu -> create ( $ collection );呼叫后,返回响应对象。
链接到文档
阅读整张纸
$ response = $ sheetsu -> read ();
$ collection = $ response -> getCollection ();读取功能允许2个参数
limit - 结果数量offset - 从n n张记录开始 # Get first two rows from sheet starting from the first row
$ response = $ sheetsu -> read ( 2 , 0 );
$ collection = $ response -> getCollection ();链接到文档
要获取符合搜索条件的行,请通过标准通过数组
# Get all rows where column 'id' is 'foo' and column 'value' is 'bar'
$ response = $ sheetsu -> search ([
' id ' => ' foo ' ,
' value ' => ' bar '
]);
$ collection = $ response -> getCollection ();
# Get all rows where column 'First name' is 'Peter' and column 'Score' is '42'
$ response = $ sheetsu -> search ([
' First name ' => ' Peter ' ,
' Score ' => ' 42 '
]);
$ collection = $ response -> getCollection ();
# Get first two row where column 'First name' is 'Peter',
# column 'Score' is '42' from sheet named "Sheet3"
$ response = $ sheetsu -> search ([
' First name ' => ' Peter ' ,
' Score ' => ' 42 '
], 2 , 0 );
$ collection = $ response -> getCollection ();
# Get first two row where column 'First name' is 'Peter',
# column 'Score' is '42' from sheet named "Sheet3"
# with ignore case
$ response = $ sheetsu -> search ([
' First name ' => ' Peter ' ,
' Score ' => ' 42 '
], 2 , 0 , true );
$ collection = $ response -> getCollection ();链接到文档
要更新行,将列名称及其值传递,该名称用于查找行和模型的数组或模型,并具有要更新的数据。
# Update all columns where 'name' is 'Peter' to have 'score' = 99 and 'last name' = 'Griffin'
$ model = Model:: create ([ ' score ' => ' 99 ' , ' last name ' => ' Griffin ' ]);
$ response = $ sheetsu -> update ( ' name ' , ' Peter ' , $ model );默认情况下,发送补丁请求,该请求仅更新传递给该方法的集合中的值。要发送PUT请求,请通过第4个参数为true 。阅读更多有关Sheetsu文档中PUT和PATCH之间的区别的信息。
链接到文档
要删除行,传递列名及其值,用于查找行。
# Delete all rows where 'name' equals 'Peter'
$ response = $ sheetsu -> delete ( ' name ' , ' Peter ' );如果您需要更改要处理的工作表,可以使用表功能并通过新的Sheetsu ID来完成此操作
# Change active sheetsu to 'THIS'
$ sheetsu -> sheet ( ' THIS ' )您还可以与他人链接此功能,例如:
# Change active sheetsu to 'THIS'
$ sheetsu -> sheet ( ' THIS ' )-> read ();如果您需要使用整个电子表格很容易:
# Back to whole spreadsheet
$ sheetsu -> whole ()您还可以与他人链接此功能,例如:
# Back to whole spreadsheet
$ sheetsu -> whole ()-> read ();Sheetsu PHP库通过连接类处理连接。该类使用卷曲来建立连接,并将响应类用作返回。响应对象是负责给予收集,模型或错误(或上次呼叫的任何其他响应)错误处理的对象(响应对象(响应使用ErrorHandler类)来抽象try/catch块,并与errorexception php类紧密耦合)
$ response = $ sheetsu -> read ();
#if you need only the error messages, you can get the errors like this
$ errors = $ response -> getErrors ();
$ firstError = $ response -> getError ();
#if you need to get the exceptions thrown, do it like this.
$ exceptions = $ response -> getExceptions ();
$ firstException = $ response -> getException ();该库的代码覆盖率超过97%。其中一些测试不是使用模拟对象,这是在我们的待办事项列表中,希望我们这样做。测试准备与PHPUNIT一起使用,测试套件是通过XML配置的,因此您只需要在此存储库的分叉版本中执行phpunit。
./vendor/bin/phpunit