Guest User

Untitled

a guest
Mar 6th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2. require_once 'Zend/Loader.php';
  3. Zend_Loader::loadClass('Zend_Http_Client');
  4. Zend_Loader::loadClass('Zend_Gdata');
  5. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  6. Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
  7.  
  8. class GSpreadsheet
  9. {
  10. public $gClient;
  11.  
  12. public $username;
  13. public $password;
  14. public $spreadsheetName;
  15. public $worksheetName;
  16.  
  17. private $_spreadsheetId;
  18. private $_worksheetId;
  19.  
  20. function __construct($username, $password, $spreadsheetName, $worksheetName)
  21. {
  22. $this->username = $username;
  23. $this->password = $password;
  24. $this->spreadsheetName = $spreadsheetName;
  25. $this->worksheetName = $worksheetName;
  26.  
  27. $authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  28. $httpClient = Zend_Gdata_ClientLogin::getHttpClient($this->username, $this->password, $authService);
  29.  
  30. $this->gClient = new Zend_Gdata_Spreadsheets($httpClient);
  31.  
  32. $this->_init();
  33. }
  34.  
  35. private function _init()
  36. {
  37. // Get spreadsheets and pick the specified spreadsheet name
  38. $feed = $this->gClient->getSpreadsheetFeed();
  39. foreach($feed->entries as $entry)
  40. {
  41. if ($entry->title->text == $this->spreadsheetName)
  42. {
  43. $this->_spreadsheetId = split("/", $entry->id->text);
  44. $this->_spreadsheetId = $this->_spreadsheetId[count($this->_spreadsheetId) - 1];
  45. }
  46. }
  47.  
  48. // Get worksheets in spreadsheet and select the specified worksheet name
  49. $docQuery = new Zend_Gdata_Spreadsheets_DocumentQuery();
  50. $docQuery->setSpreadsheetKey($this->_spreadsheetId);
  51. $feed = $this->gClient->getWorksheetFeed($docQuery);
  52. foreach($feed->entries as $entry)
  53. {
  54. if ($entry->title->text == $this->worksheetName)
  55. {
  56. $this->_worksheetId = split("/", $entry->id->text);
  57. $this->_worksheetId = $this->_worksheetId[count($this->_worksheetId) - 1];
  58. }
  59. }
  60.  
  61. return (empty($this->_spreadsheetId) || empty($this->_worksheetId)) ? false : true;
  62. }
  63.  
  64. public function insertRow($rowData)
  65. {
  66. if ((empty($this->_spreadsheetId)) && (empty($this->_worksheetId))) $this->_init();
  67.  
  68. $entry = $this->gClient->insertRow($rowData, $this->_spreadsheetId, $this->_worksheetId);
  69.  
  70. return ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) ? $entry : null;
  71. }
  72.  
  73. public function getWorksheetItemCount()
  74. {
  75. $listQuery = new Zend_Gdata_Spreadsheets_ListQuery();
  76. $listQuery->setSpreadsheetKey($this->_spreadsheetId);
  77. $listQuery->setWorksheetId($this->_worksheetId);
  78. $feed = $this->gClient->getListFeed($listQuery);
  79.  
  80. return $feed->count();
  81. }
  82. }
  83.  
  84. class GSpreadsheetRow
  85. {
  86. public $columns = array();
  87.  
  88. public function addColumn($name, $data = null)
  89. {
  90. // Fix the column name to be only alpha-numeric/no-whitespace/lowercase
  91. $name = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $name));
  92. $this->columns[$name] = $data;
  93.  
  94. return $this;
  95. }
  96. }
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // Sample Usage of GSpreadSheet
  100. $gs = new GSpreadsheet('username', 'password', 'spreadsheet-name', 'worksheet-name');
  101.  
  102. $row = new GSpreadsheetRow();
  103. $row->addColumn('My Column 1', ($gs->getWorksheetItemCount() + 1))
  104. ->addColumn('My Column 2', date('m/d/Y'))
  105. ->addColumn('My Column (3)', 100000);
  106.  
  107. if ($gs->insertRow($row->columns))
  108. echo "Row inserted successfully.";
  109. else
  110. echo "Errors while inserting row!";
  111. ?>
Add Comment
Please, Sign In to add comment