在使用 PHP 在 google 文档 api 中创建 Header/Footer 之后,我们如何将文本插入 Header/Footer?

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

我正在处理 google 文档 api。我想使用 google 文档 API 和 PHP.

在 google 文档中插入 header/footer 中的内容

所以,首先我通过以下请求创建 header/Footer 请求-

$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);

但是,现在我想在上面创建的 header/footer 中插入一些文本。为此,我这样做了-

$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);

我想通过再次更新我的批次,我可以在我的 header/footer 中插入文本,但不幸的是,这不是正确的方法,我被困在这个 point.If 这不是方法那么我们如何使用 PHP.

将文本插入 google 文档中的 header/footer

建议

也许您可以尝试构建类似于

的答案的 $requests 代码

示例调整代码:

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

当文本插入到页眉和页脚时,我认为有一个重要的点。为新 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);

参考文献: