【问题标题】:Is it possible to insert a Horizontal Rule with Google Docs API?是否可以使用 Google Docs API 插入水平规则?
【发布时间】:2019-12-27 14:10:06
【问题描述】:

我一直在从事一个项目,该项目需要使用 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 支持 insertHorizo​​ntalRule 但 Docs API 似乎没有等效项。我在这里、Google 和 API 文档中进行了搜索,但找不到任何参考。有人可以告诉我是否可能吗?如果可能,正确的请求类型是什么?

没有记录的插入方式似乎也很奇怪,但是您可以查询现有文档的内容,并且文档中的任何内容都会作为其结构的一部分报告给您。

为了清楚起见,我试图将一个 Google Doc 的内容附加到另一个。如果有人知道比逐个使用源文档元素并创建将这些元素添加到目标文档的请求更好的方法,那将绕过处理插入水平规则的需要。

【问题讨论】:

  • 很遗憾,目前阶段,Google Docs API 中似乎还没有添加水平线的方法,而“horizo​​ntalRule”可以通过documents.get 获取。 Docs API 现在正在增长。所以这可能会在未来的更新中添加。作为当前的解决方法,使用 Google Apps 脚本创建的 Web 应用程序作为 API 怎么样?这样,可以使用 php 添加水平规则。如果这不是您想要的方向,我很抱歉。
  • 感谢@Tanaike,虽然不是我想听到的,但我怀疑是这样。希望他们在不久的将来会添加。我会看看你的建议,我也会看看可能插入一个带有顶部边框线的薄表来代替水平线。请不要道歉,您已经回答了问题并给出了可能的解决方法,这可能对其他人和我都有帮助。如果您将您的评论作为答案重新发布,我会将其设为已接受的答案。
  • 感谢您的回复。我使用 Web Apps 作为答案发布了解决方法。你能确认一下吗?而且我认为您的I'm also going to look at possible inserting a thin table with a top border line as a replacement for a horizontal rule. 建议可以用作解决方法。这可以使用当前的 Docs API 来实现。所以我把它作为一个答案。也请您确认一下吗?

标签: php google-apps-script google-docs google-docs-api


【解决方案1】:
  • 您想使用 Docs API 将水平线插入 Google 文档。
  • 您想使用 php 来实现。
  • 您已经能够使用 Docs API 获取和放置 Google Document 的值。

我可以像上面那样理解。不幸的是,在现阶段,Google Docs API 中似乎还没有添加水平规则的方法,而“horizo​​ntalRule”可以通过documents.get 检索。 Docs API 现在正在增长。所以这可能会在未来的更新中添加。

所以在当前阶段,需要使用解决方法。

模式一:

在此模式中,使用由 Google Apps Script 创建的 Web Apps 作为 API 将水平线添加到 Google Document。

用法:

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。部署网络应用:

  1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
  2. “执行应用程序为:”选择“我”
  3. “谁有权访问应用程序:”选择“任何人,甚至匿名”
    • 此设置用于测试情况。
    • 您还可以通过设置“仅限我自己”而不是“任何人,甚至匿名”来使用访问令牌进行访问。
  4. 单击“部署”按钮作为新的“项目版本”。
  5. 自动打开“需要授权”对话框。
    1. 点击“查看权限”。
    2. 选择自己的帐户。
    3. 点击“此应用未验证”中的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 点击“允许”按钮。
  6. 点击“确定”。
  7. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

3。将 Web 应用用作 API:

以下脚本适用于 PHP。请设置idindex的查询参数。 id 是 Google 文档 ID。当设置index=1 时,水平线插入到 Document 的正文顶部。在本例中,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. 建议也可以作为解决方法。为此,脚本如下。

运行此脚本时,将在文档顶部创建仅包含 top 行的新表。在运行脚本之前,请设置$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);

参考资料:

【讨论】:

  • 对于我的情况,我认为第二种方法是最合适的。它允许我将它插入到我的其他 batchUpdate 请求中,并且在更新 API 以支持真正的水平规则时将需要最少的更改。我已经尝试了您的代码,并且效果很好。我在我的实现中做了一个改变——我没有改变borderBottom的样式,而是改变了borderTop,这更接近于水平规则的外观(HR上方的间距比下方的间距大),我的下一步将表格单元格的字体高度更改为 3pt。
  • @Scott Markham 感谢您的回复和其他信息。
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多