1

I need to create spreadsheet using google drive PHP API, how can i do that ? i already have code put it always get untitled file and when i click on it there is no data here is my code:

/**
* Insert new file.
*
* @param apiDriveService $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 DriveFile The file that was inserted. NULL is returned if an API error occurred.
*/
function insertFile($service, $title, $description, $parentId, $mimeType, $data) {
$file = new DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);

// Set the parent folder.
if ($parentId != null) {
    $parentsCollectionData = new DriveFileParentsCollection();
    $parentsCollectionData->setId($parentId);
    $file->setParentsCollection(array($parentsCollectionData));
}

try {

    $createdFile = $service->files->insert($file, array(
        'data' => $data,
        'mimeType' => $mimeType,
    ));

    // Uncomment the following line to print the File ID
    // print 'File ID: %s' % $createdFile->getId();

    print_r($createdFile);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
}
if(isset($_POST['insertFile'])){
$service = new apiDriveService($client);
insertFile($service,'Google Maps Mrakers.txt','ADD Google Map Mrakers To File',NULL,'text/plain',$_POST['data']);
exit;
}

1 Answer 1

4

You are having this issue because you are using an out-of-date version of the PHP client library. Please sync your client to the trunk of the project repository.

You would have to re-write some of your code as the client library has been refactored since you first wrote your code. The reference guides in the Google Drive SDK documentation have been updated to reflect this refactoring.

To create a Google Spreadsheet, insert a file without content with its mimeType set to application/vnd.google-apps.spreadsheet.

Sign up to request clarification or add additional context in comments.

3 Comments

Simply set the file's mimeType to application/vnd.google-apps.spreadsheet: this will create an empty spreadsheet in the user's Google Drive.
Thx @Alain for your effort your all answers are correct, put how can i put content in that empty spreadsheet ?
@AhmedSaber, you will need to use the Spreadsheets API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.