Skip to content
Advertisement

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

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 GoogleServiceException: {
  "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.

Advertisement

Answer

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:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement