是否可以使用 Google 文档 API 插入水平线?
Is it possible to insert a Horizontal Rule with Google Docs API?
我一直在从事一个项目,该项目需要使用 PHP 将文本和其他类型的元素插入到 Google Docs 文档中。我可以使用以下代码插入文本:
$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 使用文档 API 的文档中 API。
- 您想使用 php 实现此目的。
- 您已经能够使用 Docs API.
获取和放置 Google 文档的值
我可以像上面那样理解。不幸的是,目前阶段,Google文档API中似乎还没有添加水平线的方法,而"horizontalRule"可以通过documents.get
检索。文档 API 现在正在增长。所以这可能会在以后的更新中添加。
因此在现阶段,需要使用解决方法。
模式 1:
在此模式中,水平线被添加到 Google 文档中,使用由 Google Apps 脚本创建的 Web 应用程序作为 API。
用法:
1. 设置 Web Apps 脚本:
请将以下脚本复制并粘贴到 Google Apps 脚本的脚本编辑器中。
function doGet(e) {
DocumentApp.openById(e.parameter.id).getBody().insertHorizontalRule(Number(e.parameter.index) - 1);
return ContentService.createTextOutput("Done");
}
2。部署 Web 应用程序:
- 在脚本编辑器上,通过"Publish" -> "Deploy as web app"打开一个对话框。
- Select "Me" 对于 "Execute the app as:".
- Select "Anyone, even anonymous" 对于 "Who has access to the app:"。
- 此设置用于测试情况。
- 您还可以通过设置 "Only myself" 而不是 "Anyone, even anonymous" 来使用访问令牌进行访问。
- 单击 "Deploy" 按钮作为新按钮 "Project version"。
- 自动打开"Authorization required"的对话框。
- 点击"Review Permissions"。
- Select自己的账号。
- 在 "This app isn't verified" 单击 "Advanced"。
- 点击"Go to ### project name ###(unsafe)"
- 单击 "Allow" 按钮。
- 点击"OK"。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec
。
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
3。将 Web 应用程序用作 API:
以下脚本适用于 PHP。请设置id
和index
的查询参数。 id
是 Google 文档 ID。当设置 index=1
时,水平线被插入到 Document 中 body 的顶部。在这种情况下,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);
模式二:
我认为您的 I'm also going to look at possible inserting a thin table with a top border line as a replacement for a horizontal rule.
提议也可以用作解决方法。为了实现这一点,脚本如下。
当这个脚本是 运行 时,新的 table 只有顶部的行被创建到文档的顶部。在你运行脚本之前,请设置$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);
参考文献:
我一直在从事一个项目,该项目需要使用 PHP 将文本和其他类型的元素插入到 Google Docs 文档中。我可以使用以下代码插入文本:
$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 使用文档 API 的文档中 API。
- 您想使用 php 实现此目的。
- 您已经能够使用 Docs API. 获取和放置 Google 文档的值
我可以像上面那样理解。不幸的是,目前阶段,Google文档API中似乎还没有添加水平线的方法,而"horizontalRule"可以通过documents.get
检索。文档 API 现在正在增长。所以这可能会在以后的更新中添加。
因此在现阶段,需要使用解决方法。
模式 1:
在此模式中,水平线被添加到 Google 文档中,使用由 Google Apps 脚本创建的 Web 应用程序作为 API。
用法:
1. 设置 Web Apps 脚本:请将以下脚本复制并粘贴到 Google Apps 脚本的脚本编辑器中。
function doGet(e) {
DocumentApp.openById(e.parameter.id).getBody().insertHorizontalRule(Number(e.parameter.index) - 1);
return ContentService.createTextOutput("Done");
}
2。部署 Web 应用程序:
- 在脚本编辑器上,通过"Publish" -> "Deploy as web app"打开一个对话框。
- Select "Me" 对于 "Execute the app as:".
- Select "Anyone, even anonymous" 对于 "Who has access to the app:"。
- 此设置用于测试情况。
- 您还可以通过设置 "Only myself" 而不是 "Anyone, even anonymous" 来使用访问令牌进行访问。
- 单击 "Deploy" 按钮作为新按钮 "Project version"。
- 自动打开"Authorization required"的对话框。
- 点击"Review Permissions"。
- Select自己的账号。
- 在 "This app isn't verified" 单击 "Advanced"。
- 点击"Go to ### project name ###(unsafe)"
- 单击 "Allow" 按钮。
- 点击"OK"。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec
。- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
3。将 Web 应用程序用作 API:
以下脚本适用于 PHP。请设置id
和index
的查询参数。 id
是 Google 文档 ID。当设置 index=1
时,水平线被插入到 Document 中 body 的顶部。在这种情况下,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);
模式二:
我认为您的 I'm also going to look at possible inserting a thin table with a top border line as a replacement for a horizontal rule.
提议也可以用作解决方法。为了实现这一点,脚本如下。
当这个脚本是 运行 时,新的 table 只有顶部的行被创建到文档的顶部。在你运行脚本之前,请设置$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);