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

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



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

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


関連する記事

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



<?
//**********************************************************
// Zend Gdata による Google ドキュメントのセルの更新
// 
// ※ demos\Zend\Gdata\Spreadsheet-ClientLogin.php
// ※ を書きなおしたものです
// 
// include_path="インストールパス\ZendFramework\library"
// Windows : extension=php_openssl.dll が必要です
//**********************************************************
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
Zend_Loader::loadClass('Zend_Http_Client');

//**********************************************************
// https が使用されます。
//**********************************************************
$email = "メールアド゜レス";
$password = "パスワード";
$target_no = 0;
// 更新するセルの位置
$row = 1;
$col = 1;
// 更新するセルの値
$inputValue = "lightbox";

//**********************************************************
// コンストラクタで行われていた処理
// ※ 接続
//**********************************************************
try {
	$client = Zend_Gdata_ClientLogin::getHttpClient(
		$email,
		$password,
		Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME
	);
}
catch (Zend_Gdata_App_AuthException $ae) {
  exit("Error: ". $ae->getMessage() ."\n資格情報が必要です\n");
}

$gdClient	= new Zend_Gdata_Spreadsheets($client);
$currKey	= '';
$currWkshtId	= '';
$listFeed	= '';
$rowCount	= 0;
$columnCount	= 0;

//**********************************************************
// promptForSpreadsheet( Spreadsheet の選択 )
//**********************************************************
$feed = $gdClient->getSpreadsheetFeed();

// $feed の表示
$targetEntry1 = null;
$i = 0;
foreach($feed->entries as $entry) {
	if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
		print $entry->title->text
			 .' '. $entry->content->text . " : CellEntry<br />\n";
	}
	else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
		print $i .' '. $entry->title->text
			 .' | '. $entry->content->text . " : ListEntry<br />\n";
	}
	else {
		// ここが使われています
//		print $i .' '. $entry->title->text . " : title<br />\n";
		$targetEntry1[] = array($entry->title->text,$i);
	}
	$i++;
}

// タイトルでソートして表示
// この順序を使用して更新します
sort($targetEntry1);
foreach($targetEntry1 as $entry) {
	print $entry[1] .' '. $entry[0] . " : title<br />\n";
}

print "feed の数 : $i<br /><br />\n";

print "対象データ番号 : $target_no<br />\n";
print "対象データ : " . $feed->entries[$targetEntry1[$target_no][1]]->id->text . "<br />\n";

// キーとして保存
$currKey = explode('/', $feed->entries[$targetEntry1[$target_no][1]]->id->text);
$currKey = $currKey[5];

print "currKey : $currKey<br />\n";
print "<br />\n";


//**********************************************************
// promptForWorksheet( Worksheet の選択 )
//**********************************************************
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();

// 選択した Spreadsheet
$query->setSpreadsheetKey($currKey);

$feed = $gdClient->getWorksheetFeed($query);
// $feed の表示
$i = 0;
foreach($feed->entries as $entry) {
	if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
		print $entry->title->text
			 .' '. $entry->content->text . " : CellEntry<br />\n";
	}
	else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
		print $i .' '. $entry->title->text
			 .' | '. $entry->content->text . " : ListEntry<br />\n";
	}
	else {
		// ここが使われています
		print $i .' '. $entry->title->text . " : title<br />\n";
	}
	$i++;
}

print "feed の数 : $i<br /><br />\n";

print "先頭データ : " . $feed->entries[0]->id->text . "<br />\n";

// IDとして保存
$currWkshtId = explode('/', $feed->entries[0]->id->text);
$currWkshtId = $currWkshtId[8];

print "currWkshtId : $currWkshtId<br />\n";
print "<br />\n";

//**********************************************************
// セルの更新
//**********************************************************
$entry = $gdClient->updateCell(
	$row,
	$col,
	$inputValue,
	$currKey,
	$currWkshtId
);
if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
	print "更新が成功しました<br />\n";
	print "行 : $row, カラム : $col, 値 : $inputValue<br />\n";
}
print "<br />\n";


?>
OK














  infoboard   管理者用   
このエントリーをはてなブックマークに追加





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ