Create a Google Drive spreadsheet from a local CSV file using the Google Drive API

I am trying to upload a local CSV file to Google Drive and display it as a Google spreadsheet. However, when I go to my Google Drive and click on the link to my file, I can download it, and not view it as a spreadsheet. I tried using ?convert=true , but the file does not convert. I also tried to use application/vnd.google-apps.spreadsheet as the mime type, but noting the changes or getting a 400 Bad request response.

When I right-click the file, I can open it using Google Spreadsheets, which then displays the file correctly. I cannot find anything about this in the current documentation on Google, and a google search will not help much.

What I have done so far is creating a new empty Google spreadsheet and trying to populate it with my CSV file, but this gives me a 500 Internal Error .

  $file = new Google_DriveFile(); $file->setTitle('Exported data from ' . $this->event->title); $file->setDescription('Exported data from ' . $this->event->title); $file->setMimeType( 'text/csv' ); try { $createdFile = $this->drive->files->insert($file, array( 'data' => $data, 'mimeType' => 'application/vnd.google-apps.spreadsheet' ), array('convert'=>true)); // Uncomment the following line to print the File ID // print 'File ID: %s' % $createdFile->getId(); $additionalParams = array( 'newRevision' => false, 'data' => $data, 'convert'=>true //no change if this is true or false ); $newFile = $this->drive->files->get( $createdFile->getId() ); $newFile->setMimeType('text/csv'); $updated = $this->drive->files->update($createdFile->getId(), $newFile, $additionalParams); preint_r($updated); } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); } 

I looked at the API for Google Drive, but did not find anything useful. I am wondering if I should use the Google Spreadsheets API or the Google Drive API, which will be used exclusively?

Thanks a lot in advance, Waldemar

+4
source share
3 answers

Try the following. It is from a file: paste the link into the Google documentation, but I added a conversion parameter:

 /** * Insert new file. * * @param Google_DriveService $service Drive API service instance. * @param string $title Title of the file to insert, including the extension. * @param string $description Description of the file to insert. * @param string $parentId Parent folder ID. * @param string $mimeType MIME type of the file to insert. * @param string $filename Filename of the file to insert. * @return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred. */ function insertFile($service, $title, $description, $parentId, $mimeType, $filename) { $file = new Google_DriveFile(); $file->setTitle($title); $file->setDescription($description); $file->setMimeType($mimeType); // Set the parent folder. if ($parentId != null) { $parent = new ParentReference(); $parent->setId($parentId); $file->setParents(array($parent)); } try { $data = file_get_contents($filename); $createdFile = $service->files->insert($file, array( 'data' => $data, 'mimeType' => $mimeType, 'convert' => true, )); // Uncomment the following line to print the File ID // print 'File ID: %s' % $createdFile->getId(); return $createdFile; } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); } } 
+4
source

I try to use the code above, it will not work for me, it just stops after logging in

just use $ File-> setMimeType ('app / vnd.google-apps.spreadsheet);

 $createdFile = $service->files->insert($file, array( 'data' => $data, 'mimeType' => 'text/csv', 'convert' => true, )); 

he will work for me

+2
source

I need to add another parameter for it to work. 'uploadType' => 'multipart'

 $createdFile = $service->files->insert($file,array( 'data' => $data, 'mimeType' => 'text/csv', 'convert' => true, 'uploadType' => 'multipart', )); 

Now it works.

+1
source

All Articles