PHP : Zend Gdata による Google ドキュメントのセルの更新

  デモ用のコードを解りやすく



Zend Framework
Zend Framework: Downloads
Zend Framework: Documentation: Google Spreadsheets の使用法

Google ドキュメントで最も必要な処理は、準備したデータを取り出す処理と更新する処理になります。取り出すほうは、JavaScript で、比較的簡単なのですが、更新のサンプルがやたらと難解だったので書きなおしました。


関連する記事

JS : Google Visualization : Table : Googleドキュメントを使ったテーブルの作成



001.<?
002.//**********************************************************
003.// Zend Gdata による Google ドキュメントのセルの更新
004.//
005.// ※ demos\Zend\Gdata\Spreadsheet-ClientLogin.php
006.// ※ を書きなおしたものです
007.//
008.// include_path="インストールパス\ZendFramework\library"
009.// Windows : extension=php_openssl.dll が必要です
010.//**********************************************************
011.header( "Content-Type: text/html; Charset=utf-8" );
012.header( "pragma: no-cache" );
013.header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
014.header( "Cache-control: no-cache" );
015. 
016.require_once 'Zend/Loader.php';
017.Zend_Loader::loadClass('Zend_Gdata');
018.Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
019.Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
020.Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
021.Zend_Loader::loadClass('Zend_Http_Client');
022. 
023.//**********************************************************
024.// https が使用されます。
025.//**********************************************************
026.$email = "メールアド゜レス";
027.$password = "パスワード";
028.$target_no = 0;
029.// 更新するセルの位置
030.$row = 1;
031.$col = 1;
032.// 更新するセルの値
033.$inputValue = "lightbox";
034. 
035.//**********************************************************
036.// コンストラクタで行われていた処理
037.// ※ 接続
038.//**********************************************************
039.try {
040.    $client = Zend_Gdata_ClientLogin::getHttpClient(
041.        $email,
042.        $password,
043.        Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME
044.    );
045.}
046.catch (Zend_Gdata_App_AuthException $ae) {
047.  exit("Error: ". $ae->getMessage() ."\n資格情報が必要です\n");
048.}
049. 
050.$gdClient   = new Zend_Gdata_Spreadsheets($client);
051.$currKey    = '';
052.$currWkshtId    = '';
053.$listFeed   = '';
054.$rowCount   = 0;
055.$columnCount    = 0;
056. 
057.//**********************************************************
058.// promptForSpreadsheet( Spreadsheet の選択 )
059.//**********************************************************
060.$feed = $gdClient->getSpreadsheetFeed();
061. 
062.// $feed の表示
063.$targetEntry1 = null;
064.$i = 0;
065.foreach($feed->entries as $entry) {
066.    if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
067.        print $entry->title->text
068.             .' '. $entry->content->text . " : CellEntry<br />\n";
069.    }
070.    else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
071.        print $i .' '. $entry->title->text
072.             .' | '. $entry->content->text . " : ListEntry<br />\n";
073.    }
074.    else {
075.        // ここが使われています
076.//      print $i .' '. $entry->title->text . " : title<br />\n";
077.        $targetEntry1[] = array($entry->title->text,$i);
078.    }
079.    $i++;
080.}
081. 
082.// タイトルでソートして表示
083.// この順序を使用して更新します
084.sort($targetEntry1);
085.foreach($targetEntry1 as $entry) {
086.    print $entry[1] .' '. $entry[0] . " : title<br />\n";
087.}
088. 
089.print "feed の数 : $i<br /><br />\n";
090. 
091.print "対象データ番号 : $target_no<br />\n";
092.print "対象データ : " . $feed->entries[$targetEntry1[$target_no][1]]->id->text . "<br />\n";
093. 
094.// キーとして保存
095.$currKey = explode('/', $feed->entries[$targetEntry1[$target_no][1]]->id->text);
096.$currKey = $currKey[5];
097. 
098.print "currKey : $currKey<br />\n";
099.print "<br />\n";
100. 
101. 
102.//**********************************************************
103.// promptForWorksheet( Worksheet の選択 )
104.//**********************************************************
105.$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
106. 
107.// 選択した Spreadsheet
108.$query->setSpreadsheetKey($currKey);
109. 
110.$feed = $gdClient->getWorksheetFeed($query);
111.// $feed の表示
112.$i = 0;
113.foreach($feed->entries as $entry) {
114.    if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
115.        print $entry->title->text
116.             .' '. $entry->content->text . " : CellEntry<br />\n";
117.    }
118.    else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
119.        print $i .' '. $entry->title->text
120.             .' | '. $entry->content->text . " : ListEntry<br />\n";
121.    }
122.    else {
123.        // ここが使われています
124.        print $i .' '. $entry->title->text . " : title<br />\n";
125.    }
126.    $i++;
127.}
128. 
129.print "feed の数 : $i<br /><br />\n";
130. 
131.print "先頭データ : " . $feed->entries[0]->id->text . "<br />\n";
132. 
133.// IDとして保存
134.$currWkshtId = explode('/', $feed->entries[0]->id->text);
135.$currWkshtId = $currWkshtId[8];
136. 
137.print "currWkshtId : $currWkshtId<br />\n";
138.print "<br />\n";
139. 
140.//**********************************************************
141.// セルの更新
142.//**********************************************************
143.$entry = $gdClient->updateCell(
144.    $row,
145.    $col,
146.    $inputValue,
147.    $currKey,
148.    $currWkshtId
149.);
150.if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
151.    print "更新が成功しました<br />\n";
152.    print "行 : $row, カラム : $col, 値 : $inputValue<br />\n";
153.}
154.print "<br />\n";
155. 
156. 
157.?>
158.OK













  infoboard   管理者用   





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ