How To Use Google Sheets API v4 To Create New Sheet or Tab in Spreadsheet with PHP

It looks like the documentation is saying that we must use batchUpdate from $service->spreadsheets_values collection. But that's incorrect. It should be the $service->spreadsheets collection. The proper answer after a lot of trial and error is:

try {
    $body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
        'requests' => array(
            'addSheet' => array(
                'properties' => array(
                    'title' => 'New Sheet'
                )
            )
        )
    ));
    $result1 = $service->spreadsheets->batchUpdate($sSpreadsheetID,$body);
} catch(Exception $ignore) {}

Given that you have already authenticated your API to get a $client object, and then used that to get a $service object of class Google_Service_Sheets, and given that you have assigned $sSpreadsheetID to the ID of your spreadsheet, then the above routine will attempt to add a new tab to your spreadsheet named 'New Sheet' without destroying any existing one, and will not show errors if this tab already exists. At that point, you can do something new with that new sheet by addressing it with the A1 Notation.


This is how to do it with Node.js client:

    const gsapi = google.sheets({version: 'v4', auth: client})
    const options = {
      spreadsheetId: 'YOUR ID IS EVERYTHING BETWEEN /d/ and /edit on your spreadsheets URL'
    }

    const res = await gsapi.spreadsheets.batchUpdate({
      spreadsheetId: options.spreadsheetId,
      requestBody: {
        requests: [{
          addSheet: {
            properties: {
              title: table,
            }
          }
        }]
      }
    })

    console.log(res)