View Single Post
Old 01-04-2008, 09:03 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

ok, turns out I should have fully read the first post before I started on this - I completely missed the 8-lines rule until now

As such, the code below isn't my entry - I'll be off to make an 8-line entry now - but since I wrote it, might as well post it :D

I decided to use the Google Spreadsheet service to swap the values over (using the Zend Framework GData library)

This code requires you have a valid google account, a spreadsheet created with the same name as $spreadsheet in the code and the Zend Framework installed in /library.

PHP Code:
<?php

// The challange!
$a 1;
$b 2;

// Google Account details, spreadsheet name and worksheet name
$user 'yourAccount@gmail.com';
$pass 'secret';

$spreadsheet 'talkphp_example';
$worksheet 'Sheet1';

// Load the Zend bits
set_include_path('.' PATH_SEPARATOR './library' PATH_SEPARATOR get_include_path());

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

// Connect to Google
$authService Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient Zend_Gdata_ClientLogin::getHttpClient($user$pass$authService);
$gdClient = new Zend_Gdata_Spreadsheets($httpClient);

// Fetch and set the spreadsheet
$feed $gdClient->getSpreadsheetFeed();

foreach(
$feed->entries as $entry)
{
    if (
$entry->title->text == $spreadsheet)
    {
        
$key split ('/'$entry->id->text);
        
$spreadsheetId $key[5];
    }
}

// Find and set the worksheet
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($spreadsheetId);
$feed $gdClient->getWorksheetFeed($query);

foreach(
$feed->entries as $entry)
{
    if (
$entry->title->text == $worksheet)
    {
        
$key split ('/'$entry->id->text);
        
$worksheetId $key[8];
    }
}

// Set A1 to $a and A2 to $b
$gdClient->updateCell(1,1$a$spreadsheetId$worksheetId);
$gdClient->updateCell(1,2$b$spreadsheetId$worksheetId);

// Fetch the cells
$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query->setSpreadsheetKey($spreadsheetId);
$query->setWorksheetId($worksheetId);
$cellFeed $gdClient->getCellFeed($query);

// Switch the values over!
foreach($cellFeed as $cellEntry)
{
    if (
$cellEntry->cell->getRow() == '1' && $cellEntry->cell->getColumn() == '1')
    {
        
// A1
        
$b $cellEntry->cell->getText();
    }

    if (
$cellEntry->cell->getRow() == '1' && $cellEntry->cell->getColumn() == '2')
    {
        
// A2
        
$a $cellEntry->cell->getText();
    }
}

// $a should now be '2' and $b should now be '1'
echo '$a: ' $a '<br />';
echo 
'$b: ' $b '<br />';
Dam 8 line rule! brb with a new entry :D

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
codefreek (01-05-2008)