我一直在研究一个项目,该项目需要使用PHP将文本和其他类型的元素插入到Google文档文档中。我可以使用以下代码插入文本:
$requests = [];
```
$requests[] = new \Google_Service_Docs_Request(
['insertText' => ['text' => 'Text to insert',
'location' => ['index' => $insertionIndex],
],
]);
```
$batchUpdateRequest = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$docsService->documents->batchUpdate($documentID, $batchUpdateRequest);
我也可以通过类似的调用插入分页符:
$requests = [];
```
$requests[] = new \Google_Service_Docs_Request(
['insertPageBreak' => ['location' => ['index' => $insertionIndex],
],
]);
```
$batchUpdateRequest = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$docsService->documents->batchUpdate($documentID, $batchUpdateRequest);
以上两种方法都可以正常工作(并且按照Google的建议,当我进行多次插入时,我正在向后工作)。我需要做的是向文档中添加一条水平线。我知道Google Docs允许手动插入它们,而Apps Script支持insertHorizontalRule,但是Docs API似乎没有等效功能。我已经在这里搜索过Google和API文档,但找不到任何引用。有人可以告诉我是否可能?如果可能,正确的请求类型是什么?
似乎还没有文件插入的方式,这似乎很奇怪,但是您可以查询现有文档的内容,并且文档中的任何内容都将作为结构的一部分报告给您。
为了清楚起见,我试图将一个Google文档的内容附加到另一个文档中。如果有人知道一种更好的方法,而不是逐个元素地使用源文档并创建一个将这些元素添加到目标文档的请求,那将绕过处理插入水平尺的需要。
我可以像上面那样理解。不幸的是,在当前阶段,似乎还没有方法可以在Google Docs API中添加水平规则,而[horizontalRule]可以通过documents.get
检索。 Docs API现在正在增长。因此,可以在以后的更新中添加它。
因此,在当前阶段,需要为此使用解决方法。
在此模式下,使用Google Apps脚本创建的Web Apps作为API将水平线添加到Google文档。
[请将以下脚本复制并粘贴到Google Apps脚本的脚本编辑器中。
function doGet(e) {
DocumentApp.openById(e.parameter.id).getBody().insertHorizontalRule(Number(e.parameter.index) - 1);
return ContentService.createTextOutput("Done");
}
https://script.google.com/macros/s/###/exec
。以下脚本适用于PHP。请设置id
和index
的查询参数。 id
是Google文档ID。设置为index=1
时,水平尺将插入到文档的正文顶部。在这种情况下,index
表示Google文档中的每一行。
$url = 'https://script.google.com/macros/s/###/exec?id=###&index=1'; $curl = curl_init(); $option = [ CURLOPT_URL => $url, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_FOLLOWLOCATION => true, ]; curl_setopt_array($curl, $option); $response = curl_exec($curl); $result = json_decode($response, true); curl_close($curl);
模式2:
我认为您的I'm also going to look at possible inserting a thin table with a top border line as a replacement for a horizontal rule.
建议也可以用作解决方法。为此,脚本如下。
运行此脚本时,将在文档的顶部创建仅顶部一行的新表。在运行脚本之前,请设置$documentId
。在这种情况下,请将Google_Service_Docs::DOCUMENTS
设置为范围。
$documentId = '###';
$index = 1;
$style = [
'width' => ['magnitude' => 0, 'unit' => 'PT'],
'dashStyle' => 'SOLID',
'color' => ['color' => ['rgbColor' => ['blue' => 1, 'green' => 1, 'red' => 1]]]
];
$requests = [
new Google_Service_Docs_Request([
'insertTable' => [
'location' => ['index' => $index],
'columns' => 1,
'rows' => 1
]
]),
new Google_Service_Docs_Request([
'updateTableCellStyle' => [
'tableCellStyle' => [
'borderBottom' => $style,
'borderLeft' => $style,
'borderRight' => $style,
],
'tableStartLocation' => ['index' => $index + 1],
'fields' => 'borderBottom,borderLeft,borderRight'
]
])
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest([
'requests' => $requests
]);
$result = $service->documents->batchUpdate($documentId, $batchUpdateRequest);