Home > other >  How can we insert text into Header/Footer after creating Header/Footer in google docs api using PHP?
How can we insert text into Header/Footer after creating Header/Footer in google docs api using PHP?

Time:02-10

I am working on google docs api. I want to insert content in header/footer in google docs using google docs API with PHP.

So, Firstly I am creating header/Footer request by the following request-

$requests[] = new Google_Service_Docs_Request(array(
  "createFooter" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

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

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

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

After this I am able to get the Header Id and Footer Id from the response, like this-

$footerId = $response->replies[0]->createFooter->footerId;
$headerId = $response->replies[1]->createHeader->headerId;
print("footerid".$footerId);
print("headerid".$headerId);

But, now I want to insert some texts in this above created header/footer.For that I was doing like this-

$requests[] = new Google_Service_Docs_Request(array(
  "createFooter" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

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

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

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

$footerId = $response->replies[0]->createFooter->footerId;
$headerId = $response->replies[1]->createHeader->headerId;
print("footerid".$footerId);
print("headerid".$headerId);


$requests [] = new Google_Service_Docs_Request([
  'insertText' => [
        'text' => "Sample7", 
        'location' => [
          'segmentId' => $footerId, 
          'index' => 0,
        ],
        'text' => 'sample text',
    ]
]);
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
  'requests' => $requests
));

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

I thought by updating my batch again I can able to insert text in my header/footer, but unfortunately it is not the right way and I am stuck at this point.If this is not the way then how we can insert text into header/footer in google docs using PHP.

CodePudding user response:

When the text is inserted into the header and footer, I thought that there is an important point. When the header and footer are created to the new Google Document, your batch request can be used. But when the header and footer have already been existing in the Google Document, when "createHeader" and "createFooter" requests are run, an error occurs. Because the header and footer can be created one time. I thought that this might be the reason for your issue.

If my understanding is correct, how about the following sample script?

Sample script:

$documentId = "###"; // Please set your Google Document ID.

// Check the header and footer IDs.
$obj = $service->documents->get($documentId);
$documentStyle = $obj->getDocumentStyle();
$headerId = $documentStyle->getDefaultHeaderId();
$footerId = $documentStyle->getDefaultFooterId();

// If the header or the footer are not existing, those are created. And, retrieve the header ID and the footer ID.
$requests = [];
if (empty($headerId)) {
    array_push($requests,
        new Google_Service_Docs_Request(array(
            "createHeader" => [
            "sectionBreakLocation" => [
                "index" => 0,
            ],
            "type" => "DEFAULT",
            ],
        ))
    );
}
if (empty($footerId)) {
    array_push($requests,
        new Google_Service_Docs_Request(array(
            "createFooter" => [
            "sectionBreakLocation" => [
                "index" => 0,
            ],
            "type" => "DEFAULT",
            ],
        ))
    );
}
if (count($requests) > 0) {
    $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
        'requests' => $requests
    ));
    $response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
    $r = $response->getReplies();
    foreach ($r as &$v) {
        $h = $v->getCreateHeader();
        $f = $v->getCreateFooter();
        if (!empty($h)) {
            $headerId = $h->getHeaderId();
        }
        if (!empty($f)) {
            $footerId = $f->getFooterId();
        }
    }
}

// Insert the texts to the header and footer using the header ID and footer ID.
$requests = [
    new Google_Service_Docs_Request([
        'insertText' => [
            'location' => [
                'segmentId' => $headerId,
                'index' => 0,
            ],
            'text' => 'sample text for header',
        ]
    ]),
    new Google_Service_Docs_Request([
        'insertText' => [
            'location' => [
                'segmentId' => $footerId,
                'index' => 0,
            ],
            'text' => 'sample text for footer',
        ]
    ])
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

References:

CodePudding user response:

SUGGESTION

Perhaps you can try to structure your $requests code similar to an answer from How to insert text with google docs api php

Sample Tweaked Code:

$requests = array(new Google_Service_Docs_Request(array(
    'insertText' => array(
    'text' => 'sample text',
    'location' => array(
    'segmentId' => $footerId)))));
  •  Tags:  
  • Related