How to create new google spreadsheet using Google apps script

How to create new google spreadsheet using Google apps script

Google spreadsheet is similar to Microsoft Excel sheets and it contains most of the settings similar to Excel. Using Google app script you can create a new spreadsheet by executing the script.

To start writing the script, you should launch the Google app script editor page. Ensure that you’ve logged in with your gmail account and navigate to https://script.google.com

Page open up with Untitled project and myfunction() as default function. Double click the “Untitled projectdisplayed at the top left of the page and give a Project name as you wish. Let’s say “My first Project”.

You can create new spreadsheet with two different methods. However, the name of the methods is same and it differs by number of arguments.

Method 1

This method creates a spreadsheet with given name. To create a new Spreadsheet, write the below function in the script editor page

function
myFunction() {

  var ssnew = SpreadsheetApp.create(“My
first sheet”);

 }

Method 

create(spreadsheet name)- It create spreadsheet with given name

Definition

ssnew- It denotes the variable name

SpreadsheetApp- It is the parent class of the Google Spreadsheet app.

create(“My first sheet”)- It is a method belong to SpreadsheetApp.

It allows you to create a new spreadsheet with filename “My first sheet” which is given in the parameter of this method.

Execution

Execute the script by selecting the function name (Here it’s myFunction) and click on the Run Button or go to Run and select the function name. When it prompt for authorization, click on Continue to proceed. Then, it’ll ask for your permission to access your google Drive if you’re running it for the first time. Click allow button.

Execution begins and gets completed in few seconds. You can find the “My first sheet” in your Google Drive.

This method creates a new spreadsheet called “My first sheet” with 1000 rows and 26 columns in your drive.

Method 2

This method creates a spreadsheet with given name and given number of rows and columns.

Write the below function in the same page after myFunction but make sure that existing function names are unique.

function
myFunction1() {

  var ssnew = SpreadsheetApp.create(“My
Second sheet”,100,10);

 }

Method

create(spreadsheet name, number of rows, number of columns)- It create a new spreadsheet with given name and specified number of rows and columns given in the 2nd and 3rd argument.

Execution

Go to Run and select the “myFunction1”. You’ll find a new spreadsheet “My Second sheet” with 100 rows and 10 columns in your google drive.

Please share your doubts via comments

Leave a Reply