Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading