Create a spreadsheet

This page describes how to create a spreadsheet.

Example

To create a spreadsheet, use the create method on the spreadsheets collection, as shown in the following example.

This example creates a blank spreadsheet with a specified title.

Apps Script

sheets/api/spreadsheet_snippets.gs
/**
 * Creates a new sheet using the sheets advanced services
 * @param {string} title the name of the sheet to be created
 * @returns {string} the spreadsheet ID
 */
Snippets.prototype.create = (title) => {
  // This code uses the Sheets Advanced Service, but for most use cases
  // the built-in method SpreadsheetApp.create() is more appropriate.
  try {
    let sheet = Sheets.newSpreadsheet();
    sheet.properties = Sheets.newSpreadsheetProperties();
    sheet.properties.title = title;
    const spreadsheet = Sheets.Spreadsheets.create(sheet);

    return spreadsheet.spreadsheetId;
  } catch (err) {
    // TODO (developer) - Handle exception
    Logger.log('Failed with error %s', err.message);
  }
};

Java

sheets/snippets/src/main/java/SpreadsheetSnippets.java
Spreadsheet spreadsheet = new Spreadsheet()
        .setProperties(new SpreadsheetProperties()
                .setTitle(title));
spreadsheet = service.spreadsheets().create(spreadsheet)
        .setFields("spreadsheetId")
        .execute();
System.out.println("Spreadsheet ID: " + spreadsheet.getSpreadsheetId());

JavaScript

sheets/snippets/snippets.js
gapi.client.sheets.spreadsheets.create({
  properties: {
    title: title
  }
}).then((response) => {
});

Node.js

sheets/snippets/snippets.js
const requestBody = {
  properties: {
    title,
  },
};
this.sheetsService.spreadsheets.create({
  requestBody,
  fields: 'spreadsheetId',
}, (err, spreadsheet) =>{
  if (err) {
    // Handle error.
    console.log(err);
  } else {
    console.log(`Spreadsheet ID: ${spreadsheet.spreadsheetId}`);
  }
});

PHP

sheets/snippets/src/SpreadsheetSnippets.php
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
    'properties' => [
        'title' => $title
    ]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
    'fields' => 'spreadsheetId'
]);
printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);

Python

sheets/snippets/spreadsheet_snippets.py
spreadsheet = {
    'properties': {
        'title': title
    }
}
spreadsheet = service.spreadsheets().create(body=spreadsheet,
                                            fields='spreadsheetId').execute()
print('Spreadsheet ID: {0}'.format(spreadsheet.get('spreadsheetId')))

Ruby

sheets/snippets/lib/spreadsheet_snippets.rb
spreadsheet = {
  properties: {
    title: 'Sales Report'
  }
}
spreadsheet = service.create_spreadsheet(spreadsheet,
                                         fields: 'spreadsheetId')
puts "Spreadsheet ID: #{spreadsheet.spreadsheet_id}"

Work with Google Drive folders

There’s no option to create a spreadsheet directly within a specified Drive folder using the Sheets API. By default, the created spreadsheet is saved to the user’s root folder on Drive.

However, there are 2 alternatives to saving a file to a Drive folder:

  • After the spreadsheet is created, move it to a specific folder using the files.update method of the Drive API. For more information on moving files, refer to Move files between folders.
  • Add a blank spreadsheet to a folder using the files.create method of the Drive API, specifying application/vnd.google-apps.spreadsheet as the mimeType. For more information on creating files, refer to Create a file in a folder.

For either alternative, you'll need to add the appropriate Drive API scopes to authorize the call.

To move or create a file within a shared drive folder, refer to Implement shared drive support.