Maarten Raaijmakers
Maarten Raaijmakers

Reputation: 645

Setting spreadsheet title with Guzzle in PHP

I am connected to my Google sheets, when I run a post request for https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create and set no properties, it creates a new spreadsheet.

I would love to add a title so I did this

$options = [
    'form_params' => [
        'properties' => [
            'title' => 'A new title'
        ]
    ]
];
$client->post('v4/spreadsheets', $options);

I am using Guzzle to make this call. As a response I'm getting HTTP status code 400 (Bad Request) when adding title to form params.

How can I solve this?

Upvotes: 0

Views: 273

Answers (1)

Sherif
Sherif

Reputation: 11943

You really should be using Google's SDK for this since they already have support for one in PHP.

According to their docs, sheet properties are set in a Google_Service_Sheets_SpreadsheetProperties instance which takes the title property.

An example from their docs:

<?php
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
    'properties' => [
        'title' => $title
    ]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
    'fields' => 'spreadsheetId'
]);
printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);

Upvotes: 1

Related Questions