在使用PHP在Google Docs API中创建页眉/页脚后,如何在页眉/页脚中插入文本?

人气:955 发布:2022-10-16 标签: php google-api google-docs 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

960