I’m not convinced batch processing is working:
In the code below I have a switch $batch
that if set to True
will batch all requests.
The code first searches for the text “{{title_end}}” and then uses the returned Node Ids to insert a field code before each occurence and then finally replaces all the “{{title_end}}” text with “REPLACED”.
The initial SearchRequest is never batched - all other requests are batched if $batch
is True
.
$folder = "/TestFolder";
$storage = "MY-STORAGE
$batch = false;
//create a new document to continue the process
$filename = "test-".(time() - strtotime("today")).".docx";
$copyRequest = new CopyFileRequest("$folder/$filename", "/templates/testBatchSearch.docx", $storage, $storage, NULL);
$wordsApi->copyFile($copyRequest);
//find the page block placeholders
$searchResponse = $wordsApi->search(
new SearchRequest(
$filename,
"{{_title_end_}}",
$folder,
$storage
));
//the batch array
$word_api_batch_array = [];
//insert TC fields
$tc_index = 0;
foreach( $searchResponse->getSearchResults()->getResultsList() as $searchResult ){
//get the 'title' placeholder
$nodeId = $searchResult->getRangeStart()->getNode()->getNodeId();
$request = new InsertFieldRequest(
$filename, // The filename of the input document.
new FieldInsert([
"field_code" => "{ TC \"TC-TEXT{$tc_index}\" }", // ref: https://support.microsoft.com/en-us/office/field-codes-tc-table-of-contents-entry-field-01e5dd8a-4730-4bc2-8594-23d7329e25c3
]), // \Aspose\Words\Model\FieldInsert Field data.
null, // The path to the node in the document tree.
$folder, // Original document folder.
$storage, // Original document storage.
insert_before_node: $nodeId // The index of the node. A new field will be inserted before the node with the specified node Id.
);
if( $batch ){
$word_api_batch_array[] = new BatchPartRequest($request);
}
else {
$wordsApi->insertField($request);
}
$tc_index++;
}
//remove title placeholders
$request = new ReplaceTextRequest(
$filename,
new ReplaceTextParameters([
"old_value" => "{{_title_end_}}",
"new_value" => "REPLACED"
]),
$folder,
$storage
);
if( $batch ){
$word_api_batch_array[] = new BatchPartRequest($request);
$wordsApi->batch($word_api_batch_array);
}
else {
$wordsApi->ReplaceText($request);
}
If I run the code in non batched mode it works and I get the following result: testBatchSearch-38360.docx (13.6 KB)
If I run in batched mode only one field code is inserted and the ReplaceText is not executed:testBatchSearch-38592.docx (13.6 KB)
Further more the actual field code that is correctly inserted is random (in the attached file it is { TC “TC-TEXT3” } but in my other test runs it was different field codes .
If there where errors occuring I would expect that at least the final step would complete as the documentation states:
It may happen that one of the internal requests in a batch returns an error. In this case, this error does not interrupt the execution of the entire package, and all subsequent internal requests are processed normally.
“/templates/testBatchSearch.docx” :testBatchSearch.docx (13.5 KB)