It is my first time working with the Drive and Sheets API, and I am confused about which one should be used when creating a new file. I wish to create a blank google sheets file, saved on a drive, which I can edit programmatically later. To my understanding, the Drive API is used for basic file manipulation; such as creating, deleting, and moving files. While the Sheets API is used for more direct spreadsheet manipulation; being writing data, formatting, and such. Therefore, to create a new spreadsheet file, logic tells me that the Drive API will create the spreadsheet I want. As most of the questions on here are using old iterations of the Drive API, it is difficult (at least for me) to transition those processes to the new API.
I have been following the guide provided on the Drive API documentation. Because I am creating an empty sheets file, I have altered the code by setting the initial drive contents to null, while keeping consistent with the sample. :
CreateFileActivityOptions createOptions =
new CreateFileActivityOptions.Builder()
.setInitialDriveContents(null)
.setInitialMetadata(changeSet)
.build();
return getDriveClient().newCreateFileActivityIntentSender(createOptions);
This is the complete method I used to attempt to create the file:
private void saveFileToDrive(){
Log.i(TAG, "Creating the new file");
DriveResourceClient mDriveResourceClient;
mDriveResourceClient =Drive
.getDriveResourceClient(this,GoogleSignIn.getLastSignedInAccount(this));
mDriveResourceClient.createContents()
.continueWithTask(new Continuation<DriveContents, Task<Void>>() {
@Override
public Task<Void> then(@NonNull Task<DriveContents> task) throws Exception {
return createFileIntentSender(task.getResult());
};
});
}
private Task<Void> createFileIntentSender(DriveContents driveContents){
Log.i(TAG, "New file being created");
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("New file.xls")
.setMimeType("application/vnd.google-apps.spreadsheet")
.setStarred(true)
.build();
CreateFileActivityOptions createOptions =
new CreateFileActivityOptions.Builder()
.setInitialDriveContents(null)
.setInitialMetadata(changeSet)
.build();
return mDriveClient.newCreateFileActivityIntentSender(createOptions)
.continueWith(
new Continuation<IntentSender, Void>() {
@Override
public Void then(@NonNull Task<IntentSender> task) throws Exception {
return null;
}
}
);
}
I have changed the Mime type to application/vnd.google-apps.spreadsheet so that the new file being created is a spreadsheet. What I don't understand is that when I have a direct implementation of the sample code from the API documentation, the code consistently creates a file. Why won't it create a spreadsheet? Is there something I am missing?
The Sheets API also has a create method to create a new spreadsheet. Is that the correct methodology to follow, rather than through the Drive API?