やりたいこと
タイトルの通り
前提
Google_Clientを使用できるようにしておく。
ソースコード
sheet.php
<?php
/**
* Class GoogleSheetsAPI
*/
class GoogleSheetsAPI {
/**
* @var Google_Service_Sheets
*/
protected $service;
/**
* @var array|false|string
*/
protected $spreadsheetId;
/**
* GoogleSheetsAPISample constructor.
*/
public function __construct()
{
$dotenv = new Dotenv(__dir__);
$dotenv->load();
$credentialsPath = getenv('SERVICE_KEY_JSON');
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . dirname(__FILE__) . '/' . $credentialsPath);
$this->spreadsheetId = getenv('SPREADSHEET_ID');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$client->setApplicationName('test');
$this->service = new Google_Service_Sheets($client);
}
public function createNewSheet()
{
$title = "追加シートのタイトル";
$req = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => array('addSheet' => array('properties' => array('title' => $title )))));
$response = $this->service->spreadsheets->batchUpdate($this->spreadsheetId, $req);
}
}
$a = new GoogleSheetsAPI;
$a->createNewSheet();