Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

342
Vistas
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. I also followed this link https://developers.google.com/docs/api/reference/rest/v1/documents/request but I am unable to achieve this.

$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.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Sample1\n of 'text' => 'Sample1\n', is converted to 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: The correct 'End Of Line' symbol for this platform.

Modified script:

$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
            ]
        ]
    ])
];
  • By this modification, 'Sample1' . PHP_EOL and "Sample1\n" are used as Sample\n. By this, I thought that your goal can be achieved.
  • In this case, index of the 2nd request is 9 that you have used in the first script.

References:

  • 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:

over 4 years ago · Santiago Trujillo Denunciar

0

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.

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);

Documentation:

  • PHP Google Docs API Client Library
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda