0
votes

I have a SpreadSheet on Google Drive and I'm updating the content using PHP API (https://developers.google.com/sheets/api/guides/values).

I store here reports from my projects. Each project has its sheet.

When the project has no sheet (new project), I create a new sheet for this project called by the name of a new project.

The sheets is created as a last one.

This is my current code to create a new Sheet:

$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(
  ['requests' => ['addSheet' => ['properties' => ['title' => $project_name]]]]
);

$result = $service->spreadsheets->batchUpdate($spreadsheetId, $body);

I would like to have sheets sorted alphabetically. Excluding the first sheet, where are some global statistics.

Is there any way to change the order of the sheets using Google SpreadSheet API?

2

2 Answers

1
votes

This would be the workflow:

0
votes

I just found the solution ;-)

$requests = [
    new Google_Service_Sheets_Request([
        'updateSheetProperties' => [
            'fields' => 'index',
            'properties' => [
                'sheetId' => $sheet_id,
                'index' => $i,
            ],
        ],
    ])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
    'requests' => $requests
]);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);