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.
Asked
Active
Viewed 1,803 times
1 Answers
4
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'.

Matty B
- 1,008
- 2
- 13
- 25
-
WOW! I can't believe anyone is still looking at this question. It's funny because I gave up and told people to open some test page I made that showed the urls's of the spreadsheets and Ctrl-F for the name of the sheet they made and once you find it look backwards to a slash and then take the next three letters and that is the worksheet id. I'll look at this today though! – tooshel Aug 01 '11 at 14:49
-
Let me know if you need any clarification on the script. I've been doing a lot with the Google Docs API lately and I use this to get my IDs. Should be simpler than looking through the text for slashes and the three letter ID :p – Matty B Aug 01 '11 at 14:59