2017-03-13 145 views
1

我有一个表格,它有4个数据..谷歌电子表格API和Codeigniter

现在我收到$ _POST的数据。我想把它放到谷歌电子表格使用他们的API ..但我太新手了解代码..

我只是achived把谷歌客户端API codeigniter,但我不知道如何使用它。

我的代码是这个样子..

$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, ""); 

我不知道是什么的参数是用于,所以我留空白..

我运行此代码,并得到错误Message: Undefined property: Google::$spreadsheets_values

回答

0

你错过了几件事,让我们一步一步来。

首先,你需要进行身份验证(好像你已经做了的话):

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

指定你是如何发送数据:

$valueInputOption = 'RAW'; 

创建数组(请注意0是第一列,第1列是第2列,第2列是第3列)。如果您要发送多条线路只是做这样的例子波纹管,或者如果它只是一条线走[$i],只是做$values = array(...);

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

请注意,您需要到指定的电子表格范围和标签名称:

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

于是最后发送数据

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

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

打印的响应:

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