私は、現在ログインしているユーザーに関連する情報を含む、いくつかの div ボックスを持つ一種の「ダッシュボード」というユーザー インターフェイスに取り組んでいます。彼らのカレンダー、todo リスト、およびいくつかの統計は、Google スプレッドシートから動的に取得されます。
ここで見つけました:
http://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed
特定のセルをシートから次のような URL でリクエストできること:
spreadsheets.google.com/feeds/cells/0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/1/public/basic/R3C2
Zend GData について簡単に調べてみましたが、私がやろうとしていたことよりもはるかに複雑に思えました。
その代わりに、私は 2 つの php 関数を書きました: (hours.php で)
1.)file_get_contents()
パラメータ行、列、およびシートに基づいて、生成された URL を実行します
2.) ループで最初の関数を使用して、関連付けられている列番号を見つけます与えられた名前で。
したがって、基本的には、次のような jQuery を使用して ajax リクエストを実行します。
// js 関数を開始します
function ajaxStats(fullname)
{
$.ajax({
url: "lib/dashboard.stats.php?name="+fullname,
cache: false,
success: function(html){
document.getElementById("stats").innerHTML = html;
}
});
}
// js 関数の終了
// ファイル Hours.php の開始
<?php
function getCol($name)
{
$r=1;
$c=2;
while(getCell($r,$c,1) != $name)
{ $c++; }
return $c;
}
function getCell($r, $c, $sheet)
{
$baseurl = "http://spreadsheets.google.com/feeds/cells/";
$spreadsheet = "0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/";
$sheetID = $sheet . "/";
$vis = "public/";
$proj = "basic/";
$cell = "R".$r."C".$c;
$url = $baseurl . $spreadsheet . $sheetID . $vis . $proj . $cell . "";
$xml = file_get_contents($url);
//Sometimes the data is not xml formatted,
//so lets try to remove the url
$urlLen = strlen($url);
$xmlWOurl = substr($xml, $urlLen);
//then find the Z (in the datestamp, assuming its always there)
$posZ = strrpos($xmlWOurl, "Z");
//then substr from z2end
$data = substr($xmlWOurl, $posZ + 1);
//if the result has more than ten characters then something went wrong
//And most likely it is xml formatted
if(strlen($data) > 10)
{
//Asuming we have xml
$datapos = strrpos($xml,"<content type='text'>");
$datapos += 21;
$datawj = substr($xml, $datapos);
$endcont = strpos($datawj,"</content>");
return substr($datawj, 0,$endcont);
}
else
return $data;
}
?>
//終了時間.php
//dashboard.stats.php を開始します
<?php
session_start();
// This file is requested using ajax from the main dashboard because it takes so long to load,
// as to not slow down the usage of the rest of the page.
if (!empty($_GET['name']))
{
include "hours.php";
// GetCollumn of which C#R1 = users name
$col = getCol($_GET['name']);
// then get cell from each of the sheets for that user,
// assuming they are in the same column of each sheet
$s1 = getcell(3, $col, 1);
$s2 = getcell(3, $col, 2);
$s3 = getcell(3, $col, 3);
$s4 = getcell(3, $col, 4);
// Store my loot in the session varibles,
// so next time I want this, I don't need to fetch it
$_SESSION['fhrs'] = $s1;
$_SESSION['fdol'] = $s2;
$_SESSION['chrs'] = $s3;
$_SESSION['bhrs'] = $s4;
}
//print_r($_SESSION);
?>
<!-- and finally output the information formated for the widget-->
<strong>You have:</strong><br/>
<ul style="padding-left: 10px;">
<li> <strong><?php echo $_SESSION['fhrs']; ?></strong> fundraising hours<br/></li>
<li>earned $<strong><?php echo $_SESSION['fdol']; ?></strong> fundraising<br/></li>
<li> <strong><?php echo $_SESSION['chrs']; ?></strong> community service hours<br/></li>
<li> <strong><?php echo $_SESSION['bhrs']; ?></strong> build hours <br/></li>
</ul>
//dashboard.stats.php の終了
getCol()
[hours.php]の while ループで 4 秒を失っていると思います。
これを改善し、読み込み時間を短縮するにはどうすればよいですか?
これを破棄して、Zend GData に移動する必要がありますか?
while ループの場合、スプレッドシートの各ユーザーの列番号を、ログインも認証するユーザー データベースに格納する必要がありますか?