使用 Zend Gdata API for PHP 获取工作表列表

发布于 2024-09-25 07:45:37 字数 55 浏览 5 评论 0原文

我想列出所有工作表,其中包含工作表名称和 API 中用于引用工作表的“worksheetid”。

I want to list all the worksheets with the name of the sheet and the "worksheetid" which is used in the API to reference the sheet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2024-10-02 07:45:37

下面是一个脚本,可用于输出特定电子表格的所有工作表及其 ID。它采用“name”的 URL 查询字符串,该字符串是您想要其工作表 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”的文件夹中。

Here is a script you can use to output all of the worksheets and their ids for a particular spreadsheet. It takes a URL query string of 'name' which is the name of the spreadsheet whose worksheets you want the ids of (e.g. 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>");
?>

Note that this script depends on the Zend library being at your document root in a folder named 'library'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文