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