|
<?php |
|
/** |
|
* Google Sheetへリクエストを投げる |
|
* - パス指定だけどうにかしたい |
|
*/ |
|
require_once dirname(__FILE__).'/vendor/autoload.php'; |
|
|
|
class AppendGoogleSheet { |
|
protected $service; |
|
protected $spreadsheetId; |
|
|
|
public function __construct() { |
|
// 環境によって更新するシートを変える |
|
$home_url = get_home_url(); |
|
switch ($home_url) { |
|
case 'https://example.com': |
|
$this->spreadsheetId = '[SheetID_for_production]'; |
|
break; |
|
case 'https://stg.example.com': |
|
$this->spreadsheetId = '[SheetID_for_staging]'; |
|
break; |
|
default: |
|
$this->spreadsheetId = '[SheetID_for_development]'; |
|
} |
|
|
|
// Get the API client and construct the service object. |
|
$client = $this->getClient(); |
|
$this->service = new Google_Service_Sheets($client); |
|
} |
|
|
|
/** |
|
* Returns an authorized API client. |
|
* @return Google_Client the authorized client object |
|
*/ |
|
public function getClient() { |
|
$credentialsPath = dirname(__FILE__).'/client_secret.json'; |
|
$accessToken = json_decode(file_get_contents($credentialsPath), true); |
|
|
|
$client = new Google_Client(); |
|
$client->setApplicationName('Pontact PHP'); |
|
$client->setScopes(Google_Service_Sheets::SPREADSHEETS); |
|
$client->setAuthConfig($accessToken); |
|
return $client; |
|
} |
|
|
|
/** |
|
* Appends values to a spreadsheet. |
|
* @param array $values |
|
*/ |
|
public function appendSheet(array $values) { |
|
$value = new Google_Service_Sheets_ValueRange(); |
|
$value->setValues(array( 'values' => $values)); |
|
$range = "シート1"; |
|
$params = array("valueInputOption" => "USER_ENTERED"); |
|
|
|
$result = $this->service->spreadsheets_values->append($this->spreadsheetId, $range, $value, $params); |
|
} |
|
} |
|
|
|
|
|
?> |