Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new Sheet into existing SpreadSheet? Google Sheets API with Node.js

Using Google-Sheets-API for Node.js, how would I programmatically add/create a new Sheet into an existing Spreadsheet?

enter image description here

like image 376
UFC Insider Avatar asked Feb 22 '19 03:02

UFC Insider


People also ask

How do I create a Google Sheets project using the API?

A Google Cloud Platform project with the API enabled. To create a project and enable an API, refer to Create a project and enable the API Note: For this quickstart, you are enabling the "Google Sheets API". Authorization credentials for a desktop application. To learn how to create credentials for a desktop application, refer to Create credentials.

How do I create a new sheet in Google Docs?

This is the most basic thing to do in Excel, but none of the documentation seems to say a thing about doing it in Google Docs. Show activity on this post. The bottom of the window will list your tabs on the left and have a couple options to the left of those. Clicking the + will add a new sheet. Show activity on this post.

How to install Google Sheets API with npm?

Run the following commands to install the libraries using npm: Create a file named index.js in your working directory and copy in the following code: // If modifying these scopes, delete token.json. // time. // Load client secrets from a local file. // Authorize a client with credentials, then call the Google Sheets API.

What is Google Sheets and how does it work?

Google Sheets is a great tool for collaborating and working in the cloud, but many people and services still use other spreadsheet formats like Microsoft Excel files or CSV files (comma separated value files), so often you will find you need to transfer data from one of these spreadsheets into Google Sheets.


2 Answers

It is actually possible and documented under Spreadsheets/batchUpdate/Requests#AddSheetRequest

authorize(JSON.parse(clientSecretContent), (auth) => {
    const sheets = google.sheets({ version: 'v4', auth });
    const request = {
        // The ID of the spreadsheet
        "spreadsheetId": spreadsheetId,
        "resource": {
            "requests": [{
               "addSheet": {
                    // Add properties for the new sheet
                    "properties": {
                        // "sheetId": number,
                        // "title": sheetTitleSting,
                        // "index": number,
                        // "sheetType": enum(SheetType),
                        // "gridProperties": {
                        //     object(GridProperties)
                        // },
                        // "hidden": boolean,
                        // "tabColor": {
                        //     object(Color)
                        // },
                        // "rightToLeft": boolean
                    }
                }
            }]
        }
    };

    sheets.spreadsheets.batchUpdate(request, (err, response) => {
        if (err) {
            // TODO: Handle error
        } else {
            // TODO: Handle success
        }
    });
});
like image 135
Sahar Avr Avatar answered Oct 10 '22 19:10

Sahar Avr


For posterity stake, here's a promise based (async/await) Sheets API example of how to add a sheet (tab) to a spreadsheet.

await api.spreadsheets.batchUpdate ({ 
          spreadsheetId: spreadsheetId, 
          resource: {requests: [ {addSheet: {properties: {title: tabName }}}]}});

A full example

async function addSheet (auth, spreadsheetId, tabName) {
    const api = google.sheets({version: 'v4', auth: auth});
    try {
      // Only add sheet if it doesn't already exist
      if ((await api.spreadsheets.get({spreadsheetId: spreadsheetId})).data.sheets
        .filter(sheet => sheet.properties.title === tabName).length === 0) {
        await api.spreadsheets.batchUpdate ({ 
          spreadsheetId: spreadsheetId, 
          resource: {requests: [ {addSheet: {properties: {title: tabName }}}]}});
      }
    } catch (err) {
      console.log('Sheets API Error: ' + err);
    }
}
like image 32
hashlock Avatar answered Oct 10 '22 18:10

hashlock