How can we insert text into Header/Footer after creating Header/Footer in google docs api using PHP?(在使用PHP在Google Docs API中创建页眉/页脚后,如何在页眉/页脚中插入文本?)
本文介绍了在使用PHP在Google Docs API中创建页眉/页脚后,如何在页眉/页脚中插入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Google Docs API。我想使用带PHP的Google Docs API在Google Docs中插入页眉/页脚中的内容。
因此,首先我通过以下请求创建页眉/页脚请求-
$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);
之后,我可以从响应中获得页眉ID和页脚ID,如下所示-
$footerId = $response->replies[0]->createFooter->footerId;
$headerId = $response->replies[1]->createHeader->headerId;
print("footerid".$footerId);
print("headerid".$headerId);
但是,现在我想在上面创建的页眉/页脚中插入一些文本。为此,我这样做-
$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);
我想通过再次更新我的批次,我可以在页眉/页脚中插入文本,但不幸的是,这不是正确的方法,我被困在这里。如果不是这样,那么我们如何使用PHP在Google Docs中将文本插入到页眉/页脚中。
推荐答案
当文本插入页眉和页脚时,我认为有一个要点。当为新的Google文档创建页眉和页脚时,可以使用您的批处理请求。但是,如果页眉和页脚已经存在于Google文档中,当运行";createHeader";和";createFooter";请求时,就会发生错误。因为页眉和页脚可以一次创建。我认为这可能是您出现问题的原因。
如果我的理解正确,下面的示例脚本如何?
示例脚本:
$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);
引用:
- Method: documents.get
- Method: documents.batchUpdate
这篇关于在使用PHP在Google Docs API中创建页眉/页脚后,如何在页眉/页脚中插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在使用PHP在Google Docs API中创建页眉/页脚后,如何在页眉/页脚中插入文本?
猜你喜欢
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01