I’m building an application that adds different content types to Google Documents through the API using PHP. Adding regular text works perfectly with this approach:
$updateRequests = [];
$updateRequests[] = new \Google_Service_Docs_Request(
['insertText' => ['text' => 'My content here',
'location' => ['index' => $position],
],
]);
$batchUpdate = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $updateRequests]);
$service->documents->batchUpdate($docId, $batchUpdate);
Page breaks also work using a similar method:
$updateRequests = [];
$updateRequests[] = new \Google_Service_Docs_Request(
['insertPageBreak' => ['location' => ['index' => $position],
],
]);
$batchUpdate = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $updateRequests]);
$service->documents->batchUpdate($docId, $batchUpdate);
Both methods work great when inserting multiple items by processing them in reverse order. However, I need to insert horizontal lines into the document. While Google Docs supports adding them manually and Apps Script has insertHorizontalRule, I cannot find any equivalent in the REST API documentation.
It’s weird that you can read horizontal rules from existing documents but apparently cannot create them through the API. Has anyone found a way to insert horizontal rules? What request type should I use?
My goal is copying content from one Google Doc to another by reading each element and recreating it in the target document. If there’s a better approach than this element-by-element copying method, that would solve my horizontal rule problem too.