Reputation: 103
This is how my Spreadsheet is now .
Right now, I need to get to correct sheet by name and change color of some cells, given sheet ID and sheet name. However, I am stuck at how to get the correct sheet.
My code now:
$service = new Google_Service_Sheets($client);
$spreadSheetId = 'my_spread_sheet_id';
$spreadSheetName = 'my_sheet_day';
$sheetInfo = $service->spreadsheets->get($spreadsheetId)->getSheets();
I thought I can try getSheetsByName but it doesn't work. Looking at documentation does not have information on how to get sheet information either. Am I taking the wrong way ?
Upvotes: 1
Views: 474
Reputation: 201378
I believe your goal as follows.
In this case, to retrieve the object of properties
from each sheet using the method of "spreadsheets.get" in Sheets API. The sample script is as follows.
$service = new Google_Service_Sheets($client);
$spreadSheetId = 'my_spread_sheet_id';
$spreadSheetName = 'my_sheet_day';
// Retrieve the objects from Google Spreadsheet using Sheets API.
$sheets = $service->spreadsheets->get($spreadSheetId, ["fields" => "sheets(properties)"])->getSheets();
// Create an object for searching the sheet ID from the sheet name.
$obj = array();
foreach ($sheets as $i => $sheet) {
$property = $sheet -> getProperties();
$obj[$property -> getTitle()] = $property -> getSheetId();
}
// Retrieve the sheet ID from the sheet name.
$sheetId = $obj[$spreadSheetName];
echo $sheetId;
sheets(properties)
is used. I think that in this case, you can also use $sheets = $service->spreadsheets->get($spreadSheetId)->getSheets();
.Upvotes: 1