gpt4 book ai didi

php - 我们如何使用带有 php 的 google docs api 在 google docs 中插入多个文本和段落?

转载 作者:行者123 更新时间:2023-12-05 04:35:31 25 4
gpt4 key购买 nike

我想在具有多个段落内容的 google 文档中插入更多的一个文本,并且还想设置它们的样式。我也关注了这个链接 https://developers.google.com/docs/api/reference/rest/v1/documents/request但我无法做到这一点。

$requests [] = new Google_Service_Docs_Request(
[
'insertText' => [
'text' => 'Sample1\n',
'location' => [
'index' => 1
]
]
],
[
'insertText' => [
'text' => 'sample2\n',
'location' => [
'index' => 9
]
]
],
[
'updateParagraphStyle' => [
'range' => [
'startIndex' => 1,
'endIndex' => 8
],
'paragraphStyle' => [
'namedStyleType' => 'HEADING_1'
],
'fields' => 'namedStyleType'
]
],
[
'updateParagraphStyle' => [
'range' => [
'startIndex' => 9,
'endIndex' => 17
],
'paragraphStyle' => [
'namedStyleType' => 'NORMAL_TEXT'
],
'fields' => 'namedStyleType'
]
],
[
'updateTextStyle' => [
'range' => [
'startIndex' => 9,
'endIndex' => 16
],
'textStyle' => [
'link' => [
'url' => 'https://www.google.com'
]
],
'fields' => 'link'
]
]
);
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

我这样做并得到这个错误

PHP Fatal error:  Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid requests[0]: No request set.",
"errors": [
{
"message": "Invalid requests[0]: No request set.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}

谁能帮我解决这个问题。这将是一个很大的帮助,我需要它在 PHP 代码中。

最佳答案

'text' => 'Sample1\n',Sample1\n 被转换为Sample1\\n。我认为这可能是您遇到问题的原因。在这种情况下,使用 PHP_EOL 进行以下修改怎么样?

PHP_EOL: The correct 'End Of Line' symbol for this platform.

修改后的脚本:

$requests = [
new Google_Service_Docs_Request([
'insertText' => [
'text' => 'Sample1' . PHP_EOL, // Modified
'location' => [
'index' => 1
]
]
]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => 'Sample2' . PHP_EOL, // Modified
'location' => [
'index' => 9 // Modified
]
]
])
];

或者,请用双引号将文本括起来,如下所示。

$requests = [
new Google_Service_Docs_Request([
'insertText' => [
'text' => "Sample1\n", // Modified
'location' => [
'index' => 1
]
]
]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => "Sample2\n", // Modified
'location' => [
'index' => 9 // Modified
]
]
])
];
  • 通过此修改,'Sample1' 。 PHP_EOL"Sample1\n" 用作Sample\n。至此,我认为您的目标可以实现。
  • 在这种情况下,第二个请求的 index 是您在第一个脚本中使用的 9

引用资料:

  • Predefined Constants for PHP

  • Single quoted

    To specify a literal single quote, escape it with a backslash (). To specify a literal backslash, double it (\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

  • Double quoted

    If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters:

关于php - 我们如何使用带有 php 的 google docs api 在 google docs 中插入多个文本和段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71017411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com