是否可以使用Google Docs API插入水平线?

问题描述 投票:0回答:1

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

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

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

php google-docs google-docs-api
1个回答
0
投票
  • 您想使用Docs API将水平尺插入Google Document。
  • 您想使用php实现。
  • 您已经能够使用Docs API获取并放置Google文档的值。

我可以像上面那样理解。不幸的是,在当前阶段,似乎还没有方法可以在Google Docs API中添加水平规则,而[horizo​​ntalRule]可以通过documents.get检索。 Docs API现在正在增长。因此,可以在以后的更新中添加它。

因此,在当前阶段,需要为此使用解决方法。

模式1:

在此模式下,使用Google Apps脚本创建的Web Apps作为API将水平线添加到Google文档。

用法:

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应用程序:

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

    以下脚本适用于PHP。请设置idindex的查询参数。 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);
    

    参考:

  • insertHorizontalRule()
  • Method: documents.batchUpdate
  • © www.soinside.com 2019 - 2024. All rights reserved.