gpt4 book ai didi

php - 如何使用搜索和替换通过 API 修改 Google Docs 文档?

转载 作者:行者123 更新时间:2023-12-05 02:08:31 26 4
gpt4 key购买 nike

我需要一个示例,说明如何通过 API 使用 Google 文档中的现有文本修改现有文档。 documentation仅显示如何插入和删除文本,但不显示如何更新。一直在网上疯狂地寻找示例或有关如何做的指导,但没有运气。

最佳答案

终于自己弄明白了。

首先关注this video准备对 Google Docs API 的身份验证(尽管它是关于 Google 表格的,但过程基本相同)。基本上它包括以下步骤:

  • Google Developer Console 中创建项目
  • 启用 Google 文档 API
  • 创建凭据,包括用于编程访问的服务帐户
  • 与服务帐户客户电子邮件地址共享您的文档
  • 安装 Google API 的 PHP 客户端:composer require google/apiclient

然后创建如下脚本:

require_once(__DIR__ .'/vendor/autoload.php');
$client = new \Google_Client();
$client->setApplicationName('Some name'); //this name doesn't matter
$client->setScopes([\Google_Service_Docs::DOCUMENTS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ .'/googleapi-credentials.json'); //see https://www.youtube.com/watch?v=iTZyuszEkxI for how to create this file
$service = new \Google_Service_Docs($client);

$documentId = 'YOUR-DOCUMENT-ID-GOES-HERE'; //set your document ID here, eg. "j4i1m57GDYthXKqlGce9WKs4tpiFvzl1FXKmNRsTAAlH"
$doc = $service->documents->get($documentId);

// Collect all pieces of text (see https://developers.google.com/docs/api/concepts/structure to understand the structure)
$allText = [];
foreach ($doc->body->content as $structuralElement) {
if ($structuralElement->paragraph) {
foreach ($structuralElement->paragraph->elements as $paragraphElement) {
if ($paragraphElement->textRun) {
$allText[] = $paragraphElement->textRun->content;
}
}
}
}

// Go through and create search/replace requests
$requests = $textsAlreadyDone = $forEasyCompare = [];
foreach ($allText as $currText) {
if (in_array($currText, $textsAlreadyDone, true)) {
// If two identical pieces of text are found only search-and-replace it once - no reason to do it multiple times
continue;
}

if (preg_match_all("/(.*?)(dogs)(.*?)/", $currText, $matches, PREG_SET_ORDER)) {
//NOTE: for simple static text searching you could of course just use strpos()
// - and then loop on $matches wouldn't be necessary, and str_replace() would be simplified
$modifiedText = $currText;
foreach ($matches as $match) {
$modifiedText = str_replace($match[0], $match[1] .'cats'. $match[3], $modifiedText);
}

$forEasyCompare[] = ['old' => $currText, 'new' => $modifiedText];

$replaceAllTextRequest = [
'replaceAllText' => [
'replaceText' => $modifiedText,
'containsText' => [
'text' => $currText,
'matchCase' => true,
],
],
];

$requests[] = new \Google_Service_Docs_Request($replaceAllTextRequest);
}
$textsAlreadyDone[] = $currText;
}

// you could dump out $forEasyCompare to see the changes that would be made

$batchUpdateRequest = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

关于php - 如何使用搜索和替换通过 API 修改 Google Docs 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60809866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com