intro
Zend_Gdata_Spreadsheetを利用して、前回csvファイルより作成したスプレッドシートの内容を表示します。
詳細に関してはGoogle Spreadsheet Data APIを参照してください。
以下のプログラムを実行するにはgoogle docsを利用可能なgoogleアカウントが必要です。
一応デモサイトからも動作を確認できます(セキュリティについては詳しくは知りませんが)。
詳細に関してはGoogle Spreadsheet Data APIを参照してください。
以下のプログラムを実行するにはgoogle docsを利用可能なgoogleアカウントが必要です。
一応デモサイトからも動作を確認できます(セキュリティについては詳しくは知りませんが)。
1.コントローラの作成
以下のように
認証時に以下のスコープを指定しています。
application/constorllers/GsheetController.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php require_once 'Zend/Controller/Action.php'; require_once 'Zend/Gdata.php'; require_once 'Zend/Gdata/AuthSub.php'; require_once 'Zend/Gdata/Spreadsheets.php'; require_once 'Zend/Gdata/Spreadsheets/DocumentQuery.php'; require_once 'Zend/Gdata/Spreadsheets/ListQuery.php'; require_once 'Zend/Session/Namespace.php'; class GsheetsController extends Zend_Controller_Action { protected $_session; protected $_title = 'Test Spreadsheet'; public function preDispatch() { $this->_session = new Zend_Session_Namespace('GsheetsController'); } public function indexAction() { if (!isset($this->_session->sheet_token)) { if (isset($_GET['token'])) { // You can convert the single-use token to a session token. $token = $_GET['token']; $session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($token); // Store the session token in our session. $this->_session->sheet_token = $session_token; } else { // Display link to generate single-use token $url = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $scope = 'http://spreadsheets.google.com/feeds'; $secure = false; $session = true; $googleUri = Zend_Gdata_AuthSub::getAuthSubTokenUri( $url, $scope, $secure, $session); $this->view->message = "Go <a href='$googleUri'>Google Login Screen</a>"; return $this->render(); } } } public function viewAction() { $client = Zend_Gdata_AuthSub::getHttpClient($this->_session->sheet_token); $service = new Zend_Gdata_Spreadsheets($client); $feed = $service->getSpreadsheetFeed(); $this->view->message = 'Spreadsheet[title=' . $this->_title . '] was not found.'; foreach ($feed->entries as $entry) { if ($entry->title->text == $this->_title) { $splits = split('/', $entry->id->text); $doc_id = $splits[5]; $query = new Zend_Gdata_Spreadsheets_DocumentQuery(); $query->setSpreadsheetKey($doc_id); $feed = $service->getWorksheetFeed($query); if (count($feed->entries) > 0) { $splits = split('/', $feed->entries[0]->id->text); $sheet_id = $splits[8]; $query = new Zend_Gdata_Spreadsheets_ListQuery(); $query->setSpreadsheetKey($doc_id); $query->setWorksheetId($sheet_id); $this->view->listFeed = $service->getListFeed($query); unset($this->view->message); } } } } public function logoutAction() { if (isset($this->_session->sheet_token)) { Zend_Gdata_AuthSub::AuthSubRevokeToken($this->_session->sheet_token); unset($this->_session->sheet_token); } $this->_helper->redirector('index'); } } |
認証時に以下のスコープを指定しています。
1 |
$scope = 'http://spreadsheets.google.com/feeds'; |
2.ビューの作成
以下のように
以下のように
application/views/scripts/gsheets/index.phtml
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 |
<?php if (isset($this->message)) : ?> <?= $this->message ?> <?php else : ?> <ul> <li><a href="<?= $this->url(array('action'=>'view'))?>"> view spreadsheet</a></li> </ul> <div> <a href="<?= $this->url(array('action'=>'logout'))?>">Logout</a> </div> <?php endif; ?> |
以下のように
application/views/scripts/gsheets/view.phtml
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if (isset($this->message)) : ?> <div><?= $this->message ?></div> <?php endif; ?> <?php if (isset($this->listFeed)) : ?> <table> <tr> <?php $headerData = $this->listFeed->entries[1]->getCustom(); ?> <?php foreach($headerData as $customEntry) : ?> <th><?= $customEntry->getColumnName() ?></th> <?php endforeach; ?> </tr> <?php foreach($this->listFeed->entries as $entry) : ?> <tr> <?php $rowData = $entry->getCustom(); ?> <?php foreach($rowData as $customEntry) : ?> <td><?= $customEntry->getText() ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </table> <?php endif; ?> <div> <a href="<?= $this->url(array('action'=>'index'))?>">Back</a> </div> |
3.確認
Webサーバにアクセスし、googleアカウントでログイン後、スプレッドシートを実行してみてください。
履歴
日付 | 内容 |
---|---|
2008/5/3 | 公開 |