简体   繁体   中英

Google Spreadsheet API and Codeigniter

I have a form, it has 4 data..

Right now I got the data on $_POST. And I want to put it to Google Spreadsheet using their API.. But i'm too newbie to understand the code..

I just only achived put Google Client API to codeigniter, but I don't know how to use it.

My code is look like this..

$this->load->library('google');
$values = array(
    array(
        $this->input->post('name'),
        $this->input->post('email'),
        $this->input->post('reason'),
        $this->input->post('mobile'),
        date('Y-m-d')
    )
);

$body = new Google_Service_Sheets_ValueRange(array(
    'values' => $values
));
// $params = array(
//  'valueInputOption' => $valueInputOption
// );
$result = $this->google->spreadsheets_values->update("1gg8rHsKM3DhMaJVY-YexQyggAoq1pUCB5LpP5NFah8A", "Sheet1!A2:E2", $body, "");

I don't know what the param is intended for, so I leave it blank..

I run this code and get error Message: Undefined property: Google::$spreadsheets_values

You're missing a few things, let's go step by step.

Firstly, you need to authenticate (seems like you've done it already):

$client = $this->getClient();
$service = new Google_Service_Sheets($client);

Specify how are you sending the data:

$valueInputOption = 'RAW';

Creating the array (note that 0 is column one, 1 is column 2 and 2 is column 3). If you want to send multiple lines just do like the example bellow or if it's just one line take the [$i] and just do $values = array(...); :

$valuesb[$i] = array(
        0 => "Value 1",
        1 => "Value 2",
        2 => "Value 3"
);

Please note that you need to specify the spreadsheet range and tab name:

$data[] = new Google_Service_Sheets_ValueRange(
    array(
        'range'     => '\'TAB NAME HERE\'!A2:AU10000',
        'values'    => $valuesb
    )
);

Then finally sending the data

$requestBody = new Google_Service_Sheets_BatchUpdateValuesRequest();
$requestBody->setValueInputOption($valueInputOption);
$requestBody->setData($data);

$response = $service->spreadsheets_values->batchUpdate("Spreadsheet ID goes here", $requestBody);

Print the response:

echo "<pre>";
  print_r($response);
echo "</pre>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM