How to generate new spreadsheet file using Google Sheets API in Drive

I managed to add a new worksheet to an existing spreadsheet in my Google Drive using Java code from the official Google Sheets API documentation. However, I need to create a completely new spreadsheet file instead of just adding worksheets to existing ones.

The documentation shows how to work with existing spreadsheets but doesn’t provide clear examples for creating brand new spreadsheet files. I’ve looked at the available methods in the SpreadsheetService class but I’m not sure which approach is correct.

Can someone help me understand how to create a new spreadsheet file using the Google Sheets API?

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;

public class NewSpreadsheetCreator {
    
    public static void main(String[] args) 
            throws AuthenticationException, MalformedURLException, IOException, ServiceException {
        
        SpreadsheetService sheetService = new SpreadsheetService("MyApp");
        
        String userEmail = "USER_EMAIL";
        String userPass = "USER_PASSWORD";
        
        sheetService.setUserCredentials(userEmail, userPass);
        
        URL feedURL = new URL(
                "https://spreadsheets.google.com/feeds/spreadsheets/private/full");
        
        SpreadsheetFeed allSheets = sheetService.getFeed(feedURL, SpreadsheetFeed.class);
        List<SpreadsheetEntry> sheetList = allSheets.getEntries();
        
        for (SpreadsheetEntry sheet : sheetList) {
            System.out.println(sheet.getTitle().getPlainText());
        }
        
        SpreadsheetEntry currentSheet = sheetList.get(1);
        
        WorksheetEntry newTab = new WorksheetEntry();
        newTab.setTitle(new PlainTextConstruct("Fresh Tab"));
        newTab.setColCount(15);
        newTab.setRowCount(25);
        
        URL tabFeedUrl = currentSheet.getWorksheetFeedUrl();
        WorksheetEntry createdTab = sheetService.insert(tabFeedUrl, newTab);
    }
}

Your code’s using the old GData API that’s been deprecated for years. You need to switch to Google Sheets API v4 and Drive API v3 for creating new spreadsheets. It involves creating the file with Drive API first, then you can manipulate it using Sheets API. After migrating recently myself, I found that you can use the Drive API’s files().create() method with the mimeType set to “application/vnd.google-apps.spreadsheet” to create the file. Make sure to grab the file ID afterwards and then pass that ID to the Sheets API. Note that the authentication method has also changed; instead of using username and password, you will need to implement OAuth2 or service account credentials. It’s advisable to consult Google’s current documentation as the older GData tutorials are outdated.

Hit this exact problem 6 months back on a doc automation project. You’re using the old GData library - Google killed support for that. Here’s what works now: two-step process with the modern APIs. Step 1: Drive API v3 creates the actual spreadsheet file. POST to the files endpoint, set MIME type to ‘application/vnd.google-apps.spreadsheet’. Step 2: Take that file ID and use Sheets API v4 to set up everything else - sheet names, formatting, initial data. Heads up: OAuth2 setup is the biggest pain since username/password auth is dead. Google Cloud Console docs for enabling APIs and generating credentials aren’t bad once you dive in.