4
votes

I'm trying to upload a local CSV-file to Google Drive and display it like a Google Spreadsheet there. However, when I go to my Google Drive and click the link to my file, I can only download it, not view it as a spreadsheet. I've tried using the ?convert=true but the file doesn't get converted. I've also tried using application/vnd.google-apps.spreadsheet as the mime type but noting changes or I get a 400 Bad request response.

When I right click the file, I can choose to open with Google Spreadsheets which then displays the file correctly. I can't find anything about this in the current documentation over at Google and searches on google haven't help a whole lot.

What I've done so far is creating a new, empty Google spreadsheet and tried filling it with my CSV file but that 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've looked at the API for Google Drive but haven't found anything useful. I'm wondering if I should use the Google Spreadsheets API or is the Google Drive API the one to use solely?

Many thanks in advance, Waldemar

3
This belongs on webapps.stackexchange.comJustin Dearing

3 Answers

4
votes

Try the following. It's from the File:insert reference in the Google documentation but I added the convert 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's 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();
  }
}
2
votes

i am try above code it will not working for me it will just stop after login

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

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

it will work for me

1
votes

I have to add another parameters to make it work. 'uploadType' => 'multipart'

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

Now it's working.