How to MODIFY a Google Docs document via API using search-and-replace?



PHP Snippet 1:

public function replaceText($search, $replace)
{
    $client = $this->getClient();
    $service = new \Google_Service_Docs($client);
    $documentId = ''; // Put your document ID here


    $e = new \Google_Service_Docs_SubstringMatchCriteria();
    $e->text = "{{".$search."}}";
    $e->setMatchCase(false);


    $requests[] = new \Google_Service_Docs_Request(array(
        'replaceAllText' => array(
            'replaceText' => $replace,
            'containsText' => $e
        ),
    ));

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

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