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

79
May 28, 2022, at 05:40 AM

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.

Answer 1

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:

  • CreateHeaderRequest
  • CreateFooterRequest
Rent Charter Buses Company
READ ALSO
Merge people profiles based on email match

Merge people profiles based on email match

I have an existing directory (php with xml datasource) which contains people information such as this:

113
Writing a new and appending a file in PHP without erasing contents

Writing a new and appending a file in PHP without erasing contents

How could one write a new line to a file in php without erasing all the other contents of the file?

101
Add proc_open $pipe inputs on the go dynamically

Add proc_open $pipe inputs on the go dynamically

I am trying to create a C program compiler + executer using PHP

85