/** * 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{letsheet=Sheets.newSpreadsheet();sheet.properties=Sheets.newSpreadsheetProperties();sheet.properties.title=title;constspreadsheet=Sheets.Spreadsheets.create(sheet);returnspreadsheet.spreadsheetId;}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}};
importcom.google.api.client.http.HttpRequestInitializer;importcom.google.api.client.http.javanet.NetHttpTransport;importcom.google.api.client.json.gson.GsonFactory;importcom.google.api.services.sheets.v4.Sheets;importcom.google.api.services.sheets.v4.SheetsScopes;importcom.google.api.services.sheets.v4.model.Spreadsheet;importcom.google.api.services.sheets.v4.model.SpreadsheetProperties;importcom.google.auth.http.HttpCredentialsAdapter;importcom.google.auth.oauth2.GoogleCredentials;importjava.io.IOException;importjava.util.Collections;/* Class to demonstrate the use of Spreadsheet Create API */publicclassCreate{/** * Create a new spreadsheet. * * @param title - the name of the sheet to be created. * @return newly created spreadsheet id * @throws IOException - if credentials file not found. */publicstaticStringcreateSpreadsheet(Stringtitle)throwsIOException{/* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));HttpRequestInitializerrequestInitializer=newHttpCredentialsAdapter(credentials);// Create the sheets API clientSheetsservice=newSheets.Builder(newNetHttpTransport(),GsonFactory.getDefaultInstance(),requestInitializer).setApplicationName("Sheets samples").build();// Create new spreadsheet with a titleSpreadsheetspreadsheet=newSpreadsheet().setProperties(newSpreadsheetProperties().setTitle(title));spreadsheet=service.spreadsheets().create(spreadsheet).setFields("spreadsheetId").execute();// Prints the new spreadsheet idSystem.out.println("Spreadsheet ID: "+spreadsheet.getSpreadsheetId());returnspreadsheet.getSpreadsheetId();}}
/** * Create a google spreadsheet * @param {string} title Spreadsheets title * @return {string} Created spreadsheets ID */asyncfunctioncreate(title){const{GoogleAuth}=require('google-auth-library');const{google}=require('googleapis');constauth=newGoogleAuth({scopes:'https://www.googleapis.com/auth/spreadsheets',});constservice=google.sheets({version:'v4',auth});constresource={properties:{title,},};try{constspreadsheet=awaitservice.spreadsheets.create({resource,fields:'spreadsheetId',});console.log(`Spreadsheet ID: ${spreadsheet.data.spreadsheetId}`);returnspreadsheet.data.spreadsheetId;}catch(err){// TODO (developer) - Handle exceptionthrowerr;}}
importgoogle.authfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.errorsimportHttpErrordefcreate(title):""" Creates the Sheet the user has access to. Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for the application. """creds,_=google.auth.default()# pylint: disable=maybe-no-membertry:service=build("sheets","v4",credentials=creds)spreadsheet={"properties":{"title":title}}spreadsheet=(service.spreadsheets().create(body=spreadsheet,fields="spreadsheetId").execute())print(f"Spreadsheet ID: {(spreadsheet.get('spreadsheetId'))}")returnspreadsheet.get("spreadsheetId")exceptHttpErroraserror:print(f"An error occurred: {error}")returnerrorif__name__=="__main__":# Pass: titlecreate("mysheet1")
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-19 UTC."],[[["This page provides code samples demonstrating how to create a blank Google Spreadsheet using the Sheets API in various programming languages."],["By default, new spreadsheets are created in the user's root Drive folder, requiring the Drive API to move them to a specific folder."],["You can alternatively create a blank spreadsheet directly within a folder using the Drive API, specifying the appropriate MIME type."],["Refer to the linked resources for information on Drive API scopes, shared drive support, and Google Sheets limitations."]]],[]]