How we can insert header and footer in google docs with google docs api using PHP code

Viewed 124

I want to insert header and footer in my google docs with google docs api in PHP code. I am doing it like this-

$requests = new Google_Service_Docs_Request(array(
            'createHeader' => [
                'type' => 'TITLE',
                'sectionBreakLocation' => [
                    'index' => 0
            ],
        ],
    )),
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

but, i am getting this error-

PHP Fatal error:  Uncaught Google\Service\Exception: {
  "error": {
    "code": 400,
    "message": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"",
    "errors": [
      {
        "message": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"",
        "reason": "invalid"
      }
    ],
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "requests[5].create_header.type",
            "description": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\""
          }
        ]
      }
    ]
  }
}

Please help me out with this, That how we can insert texts in header and footer in google docs using PHP.

1 Answers

In your script, how about the following modification?

Create header:

I thought that the reason of the error message of Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"" is due to 'type' => 'TITLE',. But when I saw your script, $requests is required to be an array. So how about the following modification?

From:

$requests = new Google_Service_Docs_Request(array(
            'createHeader' => [
                'type' => 'TITLE',
                'sectionBreakLocation' => [
                    'index' => 0
            ],
        ],
    )),
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

To:

$requests = new Google_Service_Docs_Request(array(
    'createHeader' => [
        'type' => 'DEFAULT',
        'sectionBreakLocation' => [
            'index' => 0
        ],
    ],
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => array($requests)
));

Create footer:

In this case, please replace createHeader to createFooter in the above $requests.

Note:

  • As additional information, when you want to use the first page header and footer, you can use the following request.

      $requests = new Google_Service_Docs_Request(array(
          'updateDocumentStyle' => [
              'documentStyle' => [
                  'useFirstPageHeaderFooter' => true,
              ],
              'fields' => 'useFirstPageHeaderFooter',
          ],
      ));
    

References:

Related