【问题标题】:Google Spreadsheet API and CodeigniterGoogle 电子表格 API 和 Codeigniter
【发布时间】:2017-03-13 10:06:55
【问题描述】:

我有一个表格,它有 4 个数据..

现在我得到了 $_POST 上的数据。我想用他们的 API 把它放到谷歌电子表格中。但我太新手了,无法理解代码。

我只是实现了将 Google Client 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

【问题讨论】:

    标签: php codeigniter google-spreadsheet-api


    【解决方案1】:

    你错过了一些东西,让我们一步一步来。

    首先,您需要进行身份验证(好像您已经完成了):

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

    指定您如何发送数据:

    $valueInputOption = 'RAW';
    

    创建数组(注意 0 是第一列,1 是第二列,2 是第三列)。如果您想发送多行,请按照下面的示例进行操作,或者如果只是一行,请使用 [$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>";
    

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多