• Notice regarding image hosts and attachments. See here.

Any used Google API for Drive?

Soldato
Joined
27 Dec 2005
Posts
17,317
Location
Bristol
So I've gone through the whole auth process to access Drive etc from a web app. All fine, can pull data off a spreadsheet no problem. But I'm having a mare updating/adding data to said spreadsheet.

Even Google's own documentation seems to differ, with one page stating $result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params); and another dropping the $params requirements. They don't give any real world examples (at https://developers.google.com/sheets/api/guides/values) which is really annoying for a fairly amateur coder like me.

Can anyone point me in the right direction?
 
A lot of their libraries and examples are procedurally generated, so there may be some discrepancies.

There's sort of an example here: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

My guess would be something like:

Code:
<?php
$range = 'A1:B2';
// Each inner array represents a row
$values = [
    [
        'Foo', // Writes Foo into A1
        'Bar' // Writes Bar into B1
    ],
    [
        'Baz', // Writes Baz into A2
        'FooBar' // Writes FooBar into B2
    ]
];
$body = new Google_Service_Sheets_ValueRange([
    'values' => $values
]);
$result = $service->spreadsheets_values->update($spreadsheetId, $range, $body);
printf("%d cells updated.", $result->getUpdatedCells());

Might need to use:

Code:
$params = array('valueInputOption' => 'RAW');
$result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params);
 
Last edited:
Thanks, in usual fashioned I just managed about to find the answer after posting...

Code:
$service = new Google_Service_Sheets($client);
$range = 'E' . $actualrow;
$visitvalues = [[$date,],];
$body = new Google_Service_Sheets_ValueRange(['values' => $visitvalues]);
$valueInputOption = array('USER_ENTERED');
$params = ['valueInputOption' => $valueInputOption];
$response = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params);


Works like a charm!
 
Back
Top Bottom