facebook twitter hatena line email

Php/zend framework/zend google spreadsheets

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

googleドライブのスプレットシートのデータ取得

事前準備

googledriveにスプレッドシートを用意し、 1行目にcol1、col2を追加


ソース

class GoogleSpreadSheet
{
  private $_email;
  private $_pass;
  private $_ssid;
  private $_wid;
  public function __construct($email, $pass, $ssid, $wid)
  {
    require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata_Docs');
    Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    $this->_email = $email;
    $this->_pass = $pass;
    $this->_ssid = $ssid;
    $this->_wid = $wid;
  }
  public function getSheetIds()
  {
    $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
    $client = Zend_Gdata_ClientLogin::getHttpClient($this->_email, $this->_pass, $service);
    $service = new Zend_Gdata_Spreadsheets($client);

    $docQuery = new Zend_Gdata_Spreadsheets_DocumentQuery();
    $docQuery->setSpreadsheetKey($this->_ssid);
    $sheetFeed = $service->getWorksheetFeed($docQuery);
    $ret = array();
    foreach($sheetFeed->entries as $sheetEntry) {
      $temp = array(
        "name" => $sheetEntry->getTitle(),
        "id" => $sheetEntry->getId(),
      );
      $ret[] = $temp;
    }
    return $ret;
  }
  public function printSheet()
  {
    $listQuery = new Zend_Gdata_Spreadsheets_ListQuery();
    $listQuery->setSpreadsheetKey($this->_ssid);
    $listQuery->setWorkwid($this->_wid);
    $listFeed = $service->getListFeed($listQuery);
    foreach($listFeed as $rowData) {
      $rowEntry = $rowData->getCustom();
      foreach($rowEntry as $cellData) {
        echo $cellData->getColumnName()."=".$cellData->getText().'\n';
      }
      echo '\n';
    }
  }
  public function add($row)
  {
    $service  = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
    $client = Zend_Gdata_ClientLogin::getHttpClient($this->_email, $this->_pass, $service);
    $sheet = new Zend_Gdata_Spreadsheets($client);
    $data = array(
      "date" => date('Y-m-d H', time()),
    );
    $data = array_merge($data, $row);
    
    return $sheet->insertRow($data, $this->_ssid, $this->_wid);
  }
}


$email = "sample1***@gmail.com";
$pass = "****";
$ssid = '0AtX0u**************NGtIa1E';
$wid    = "od6";
$row = array(
  "col1" => "hoge",
  "col2" => "huga"
);
$sheet = new GoogleSpreadSheet($email, $pass, $ssid, $wid);
print_r($sheet->getSheetIds(), 1);
$sheet->add($row);