Search code examples
javagoogle-drive-apigoogle-sheets-apiservice-accounts

Moving created files between folders yields strange "Shared With Me" behavior


My objective is to create a spreadsheet in a specific folder. I am creating the spreadsheet with V4 sheets API and the folder using V3 drive api in java.

However, I do not know if it is possible to create a spreadsheet in a specific folder. If it is possible (in java), please let me know.

To achieve the same functionality of creating the spreadsheet in a folder, I am creating the spreadsheet and then moving it under the folder I created earlier. I am using the example code from here https://developers.google.com/drive/api/v3/folder (Moving files between folders).

I am creating these using a service account.

When I create the folder and a spreadsheet, move the spreadsheet to that folder, then add write permissions to my google account to be able to view them in the browser, what I see is that under "Shared with me" I can see the folder F and file SS-1, as well as, inside the folder F, the same file SS-1. When I print the parents of the spreadsheet file SS-1, I only see one parent which is the folder F.

Subsequently, when I add additional files SS-2, SS-3, etc, move it under folder F and give write permissions to my google account, I am able to see all those files under the folder F only, as expected.

I cannot figure out if there is a bug in my code, since when I print the parent of file SS-1, I only see one parent, which is the file ID of folder F. Hence unsure why I see the file SS-1 both under the folder F as well as directly under "Shared with me" at the same level of the folder F.

Any pointers to debug this? Alternatively, is there an easier way to create a spreadsheet directly in a specific folder?


Solution

  • To create a file directly in a given folder, you use the Drive API (since the Sheets API is all about manipulating the content of the spreadsheet (not the metadata like "where can I find this spreadsheet"):

    With the raw REST API, this is as simple as:

    POST https://www.googleapis.com/drive/v3/files?fields=createdTime%2Cid%2Cname%2Cparents
    
    {
      "mimeType": "application/vnd.google-apps.spreadsheet",
      "parents": [
        "some folder id"
      ],
      "name": "API Explorer Sheet in Folder"
    }
    

    Resp:

    {
      "id": "your new spreadsheet's id",
      "name": "API Explorer Sheet in Folder",
      "parents": [
        "some folder id"
      ],
      "createdTime": "2018-10-03T18:30:04.554Z"
    }
    

    Demo link
    Obviously, you can supply additional request metadata to influence the created file.

    Adding a file to a folder is similarly possible to do with Google's Java client library - the Drive API's "Work with Folders" page has sample code that does exactly that for a photo. Adapting to a Google Sheets file that mimics the above POST request:

    String folderId = "some folder id";
    File fileMetadata = new File();
    fileMetadata.setName("some file title");
    fileMetadata.setParents(Collections.singletonList(folderId));
    fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");
    File file = driveService.files().create(fileMetadata)
        .setFields("id, parents, name") // properties in the response
        .execute();
    System.out.println("File ID: " + file.getId());