1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Any used Google API for Drive?

Discussion in 'HTML, Graphics & Programming' started by Russinating, Jul 25, 2018.

  1. Russinating


    Joined: Dec 27, 2005

    Posts: 14,837

    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?
  2. xbenjiiman


    Joined: Feb 4, 2011

    Posts: 576

    Location: Bradford

    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:

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

    $params = array('valueInputOption' => 'RAW');
    $result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params);
    Last edited: Jul 25, 2018
  3. Russinating


    Joined: Dec 27, 2005

    Posts: 14,837

    Location: Bristol

    Thanks, in usual fashioned I just managed about to find the answer after posting...

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