LoginSignup
0
0

More than 3 years have passed since last update.

PHP×SpreadSheetAPIで特定のSpreadSheetにシートを名前をつけて追加する

Last updated at Posted at 2019-06-24

やりたいこと

タイトルの通り

前提

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();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0