Sample code for 30+ languages & platforms
Java

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example uses a previously obtained access token having permission for the
        // Google Sheets scope.

        // In this example, Get Google Sheets OAuth2 Access Token, the access
        // token was saved to a JSON file.  This example fetches the access token from the file..
        JsonObject jsonToken = new JsonObject();
        jsonToken.loadFile("qa_data/tokens/googleSheets.json");
        if (!jsonToken.hasMember("access_token")) {
            System.out.println("No access token found.");
            return;
        }

        Http http = new Http();
        http.setAuthToken(jsonToken.stringOf("access_token"));

        // Create the following JSON:
        // The JSON code can be generated using this online tool:  Generate JSON create code
        // {
        //   "sheets": [
        //     {
        //       "properties": {
        //         "title": "Sample Tab"
        //       }
        //     }
        //   ],
        //   "properties": {
        //     "title": "Create Spreadsheet using Sheets API v4"
        //   }
        // }

        // This code generates the above JSON:
        JsonObject json = new JsonObject();
        json.updateString("sheets[0].properties.title", "Sample Tab");
        json.updateString("properties.title", "Create Spreadsheet using Sheets API v4");

        // Send the POST to create the new Google spreadsheet.
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://sheets.googleapis.com/v4/spreadsheets", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("response status code = " + resp.getStatusCode());
        System.out.println("response JSON:");

        json.load(resp.getBodyStr());
        json.setEmitCompact(false);
        System.out.println(json.emit());

        // A sample response is shown below.
        // To generate the parsing source code for a JSON response, paste
        // the JSON into this online tool: Generate JSON parsing code

        // {
        //   "spreadsheetId": "1ueEQu3WDBkIAOUhzLnY4zr6JO5SrJx0dQ-YkQlUVYD0",
        //   "properties": {
        //     "title": "Create Spreadsheet using Sheets API v4",
        //     "locale": "en_US",
        //     "autoRecalc": "ON_CHANGE",
        //     "timeZone": "Etc/GMT",
        //     "defaultFormat": {
        //       "backgroundColor": {
        //         "red": 1,
        //         "green": 1,
        //         "blue": 1
        //       },
        //       "padding": {
        //         "top": 2,
        //         "right": 3,
        //         "bottom": 2,
        //         "left": 3
        //       },
        //       "verticalAlignment": "BOTTOM",
        //       "wrapStrategy": "OVERFLOW_CELL",
        //       "textFormat": {
        //         "foregroundColor": {},
        //         "fontFamily": "arial,sans,sans-serif",
        //         "fontSize": 10,
        //         "bold": false,
        //         "italic": false,
        //         "strikethrough": false,
        //         "underline": false
        //       }
        //     }
        //   },
        //   "sheets": [
        //     {
        //       "properties": {
        //         "sheetId": 1629642057,
        //         "title": "Sample Tab",
        //         "index": 0,
        //         "sheetType": "GRID",
        //         "gridProperties": {
        //           "rowCount": 1000,
        //           "columnCount": 26
        //         }
        //       }
        //     }
        //   ],
        //   "spreadsheetUrl": "https://docs.google.com/spreadsheets/d/1ueEQu3WDBkIAOUhzLnY4zr6JO5SrJx0dQ-YkQlUVYD0/edit"
        // }
        //

        String spreadsheetId = json.stringOf("spreadsheetId");
        String propertiesTitle = json.stringOf("properties.title");
        String propertiesLocale = json.stringOf("properties.locale");
        String propertiesAutoRecalc = json.stringOf("properties.autoRecalc");
        String propertiesTimeZone = json.stringOf("properties.timeZone");
        int propertiesDefaultFormatBackgroundColorRed = json.intOf("properties.defaultFormat.backgroundColor.red");
        int propertiesDefaultFormatBackgroundColorGreen = json.intOf("properties.defaultFormat.backgroundColor.green");
        int propertiesDefaultFormatBackgroundColorBlue = json.intOf("properties.defaultFormat.backgroundColor.blue");
        int propertiesDefaultFormatPaddingTop = json.intOf("properties.defaultFormat.padding.top");
        int propertiesDefaultFormatPaddingRight = json.intOf("properties.defaultFormat.padding.right");
        int propertiesDefaultFormatPaddingBottom = json.intOf("properties.defaultFormat.padding.bottom");
        int propertiesDefaultFormatPaddingLeft = json.intOf("properties.defaultFormat.padding.left");
        String propertiesDefaultFormatVerticalAlignment = json.stringOf("properties.defaultFormat.verticalAlignment");
        String propertiesDefaultFormatWrapStrategy = json.stringOf("properties.defaultFormat.wrapStrategy");
        String propertiesDefaultFormatTextFormatFontFamily = json.stringOf("properties.defaultFormat.textFormat.fontFamily");
        int propertiesDefaultFormatTextFormatFontSize = json.intOf("properties.defaultFormat.textFormat.fontSize");
        boolean propertiesDefaultFormatTextFormatBold = json.boolOf("properties.defaultFormat.textFormat.bold");
        boolean propertiesDefaultFormatTextFormatItalic = json.boolOf("properties.defaultFormat.textFormat.italic");
        boolean propertiesDefaultFormatTextFormatStrikethrough = json.boolOf("properties.defaultFormat.textFormat.strikethrough");
        boolean propertiesDefaultFormatTextFormatUnderline = json.boolOf("properties.defaultFormat.textFormat.underline");
        String spreadsheetUrl = json.stringOf("spreadsheetUrl");
        int i = 0;
        int count_i = json.sizeOfArray("sheets");
        while (i < count_i) {
            json.setI(i);
            int propertiesSheetId = json.intOf("sheets[i].properties.sheetId");
            propertiesTitle = json.stringOf("sheets[i].properties.title");
            int propertiesIndex = json.intOf("sheets[i].properties.index");
            String propertiesSheetType = json.stringOf("sheets[i].properties.sheetType");
            int propertiesGridPropertiesRowCount = json.intOf("sheets[i].properties.gridProperties.rowCount");
            int propertiesGridPropertiesColumnCount = json.intOf("sheets[i].properties.gridProperties.columnCount");
            i = i + 1;
        }
    }
}