我想用工作表的名称和 API 中用于引用工作表的“worksheetid”列出所有工作表。
问问题
1797 次
1 回答
4
这是一个脚本,可用于输出特定电子表格的所有工作表及其 ID。它需要一个 URL 查询字符串“name”,它是您想要其工作表的 id 的电子表格的名称(例如 name-of-file.php?name=myspreadsheet)
<?php
set_include_path($_SERVER["DOCUMENT_ROOT"] . "/library/");
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
/**
* Username, password and the name of the spreadsheet we would like to use
* Note that the spreadsheet is case sensitive and white space sensitive
*/
$user = "your-gmail-account-name-at-gmail-dot-com";
$pass = "your-gmail-account-password";
if($_POST['name'])
{
$spreadsheetToFind = $_POST['name'];
}
elseif($_GET['name'])
{
$spreadsheetToFind = $_GET['name'];
}
/**
* Establish a connection to our spreadsheets and get a complete list of them
*/
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$feed = $spreadsheetService->getSpreadsheetFeed();
/**
* We loop through all of the spreadsheets until we have found the one
* declared earlier. The id of the spreadsheet is then extracted using
* basename and we store the id for use in our next query, to
* obtain all of the worksheets in the spreadsheet
*/
foreach($feed->entries as $entry)
{
$spreadsheetTitle = $entry->title->text;
if($spreadsheetTitle == $spreadsheetToFind)
{
$spreadsheetURL = $entry->id;
}
}
$spreadsheetKey = basename($spreadsheetURL);
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($spreadsheetKey);
$feed = $spreadsheetService->getWorksheetFeed($query); // now that we have the desired spreadsheet, we need the worksheets
/**
* Loop through all of our worksheets and echo
* its name as well as its id
*/
echo("<table><tr><td><strong>Spreadsheet Name:</strong></td><td>" . $spreadsheetToFind . "</td></tr><tr><td><strong>Spreadsheet ID:</strong></td><td>" . $spreadsheetKey . "</td></tr>");
foreach($feed->entries as $entry)
{
echo("<tr><td><strong>".$entry->title->text . ": </strong></td><td>".basename($entry->id)."</td></tr>");
}
echo("</table>");
?>
请注意,此脚本取决于 Zend 库,该库位于您的文档根目录中名为“library”的文件夹中。
于 2011-07-31T21:15:22.697 回答