[英]How can we insert multiple texts and paragraphs in google docs using google docs api with php?
I want to insert more the one texts in google docs with multiple paragraph contents and also want to style them.我想在具有多个段落内容的 google 文档中插入更多的一个文本,并且还想设置它们的样式。 I also followed this link https://developers.google.com/docs/api/reference/rest/v1/documents/request but I am unable to achieve this.我也点击了这个链接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);
I am doing in this way and getting this error我这样做并收到此错误
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"
}
}
Can anyone help me out with this.谁能帮我解决这个问题。 It would be a great help and i need it in PHP code.这将是一个很大的帮助,我需要它在 PHP 代码中。
Sample1\n
of 'text' => 'Sample1\n',
is converted to Sample1\\n
. 'text' => 'Sample1\ Sample1\n
'text' => 'Sample1\n',
转换为Sample1\\n
。 I thought that this might be the reason of your issue.我认为这可能是您遇到问题的原因。 In this case, how about the following modification using PHP_EOL
?在这种情况下,使用PHP_EOL
进行以下修改怎么样?
PHP_EOL
: The correct 'End Of Line' symbol for this platform.PHP_EOL
:此平台的正确“行尾”符号。
$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
]
]
])
];
or, please enclose the text by the double quotes as follows.或者,请用双引号将文本括起来,如下所示。
$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'. PHP_EOL
'Sample1'. PHP_EOL
and "Sample1\n"
are used as Sample\n
. 'Sample1'. PHP_EOL
和"Sample1\n"
用作Sample\n
。 By this, I thought that your goal can be achieved.通过这个,我认为你的目标可以实现。index
of the 2nd request is 9
that you have used in the first script.在这种情况下,第二个请求的index
是您在第一个脚本中使用的9
。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.反斜杠的所有其他实例将被视为文字反斜杠:这意味着您可能习惯的其他转义序列(例如 \r 或 \n)将按照指定的字面意思为 output,而不具有任何特殊含义。
If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters:如果字符串包含在双引号 (") 中,PHP 将解释以下特殊字符的转义序列:
I think the error is caused by the construction of the batch request.我认为错误是由批量请求的构造引起的。 The request array must contain a number of instances of Google_Service_Docs_Request
.请求数组必须包含多个Google_Service_Docs_Request
实例。
Try change this:尝试改变这个:
$requests [] = new Google_Service_Docs_Request(
[
'insertText' => [
'text' => 'Sample1\n',
'location' => [
'index' => 1
]
]
],
[
'insertText' => [
'text' => 'sample2\n',
'location' => [
'index' => 9
]
]
],
To this:对此:
$requests = [
new Google_Service_Docs_Request([
'insertText' => [
'text' => 'Sample1\n',
'location' => [
'index' => 1
]
]
]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => 'sample2\n',
'location' => [
'index' => 9
]
])
];
And after proceed as follows:然后进行如下操作:
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$result = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.