Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP创建时向Google电子表格添加数据_Php_Google Sheets_Google Drive Api - Fatal编程技术网

使用PHP创建时向Google电子表格添加数据

使用PHP创建时向Google电子表格添加数据,php,google-sheets,google-drive-api,Php,Google Sheets,Google Drive Api,我正在使用PHP和Google Drive API在Google Drive中动态创建一个文件。api允许您在创建数据时添加数据,我知道以后可以使用电子表格api添加数据,但我想动态添加数据,这是我的代码 $service = new Google_Service_Drive($client); $file = new Google_Service_Drive_DriveFile(); $file->setTitle( 'Hello world!' ); $file->setDesc

我正在使用PHP和Google Drive API在Google Drive中动态创建一个文件。api允许您在创建数据时添加数据,我知道以后可以使用电子表格api添加数据,但我想动态添加数据,这是我的代码

$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle( 'Hello world!' );
$file->setDescription('A test document');
$data = "Header1,Header2,Header2,"; // CSV
$file->setMimeType( 'application/vnd.google-apps.spreadsheet' );
    $file = $service->files->insert( $file , array(
  'data' => $data,
  'convert' => true, ));
这是我的代码,它成功地创建了带有标题和说明的文件,但没有添加标题,我是否遗漏了什么?

首先设置Sheets API。它包括您需要的身份验证和授权过程。 然后在上签出指南

当我在电子表格上书写时,我在JS中使用了XHR请求。这取决于你用PHP来做,可能会更简单。 我的xhr请求主体看起来像:

         var params = {
           "range":"Sheet1!A1",
           "majorDimension": "ROWS",
           "values": [
           ["This is a test"]
          ],
         }

只需记下您的电子表格ID和电子表格ID。您可以在电子表格URL中找到打开的工作表的名称。

您可能忘记了什么,或者$service定义在哪里?@RogerWayne我刚刚粘贴了这个片段,因为文件创建得很好,所以假设$service定义得没有问题,我将进行更新。您是说使用PHP在电子表格单元格上编写吗?@noogui当然可以,除了向单元格中写入数据,我们还可以向电子表格中添加其他数据。@noogui是的,已经解决了
         var params = {
           "range":"Sheet1!A1",
           "majorDimension": "ROWS",
           "values": [
           ["This is a test"]
          ],
         }