Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create a Google Docs spreadsheet WITH CONTENT?

I have found multiple StackOverflow questions dealing with how to create or edit Google Doc spreadsheets using the Google Spreadsheets API, or older API's. However, this Spreadsheets API seems to be part of the "gdata" library, which to my understanding is deprecated.

Newer StackOverflow answers show how to create an empty spreadsheet using the Drive API, which seems more current. However, from looking at the documentation and examples for that API, it seems to only let you create new EMPTY files with the spreadsheet MIME type. I have not found any functionality for creating a spreadsheet with actual content (i.e. rows, columns, worksheets, etc).

What is the current process for creating a new Google Doc spreadsheet AND populating it with content? Does the Drive API have functionality that I'm not understanding? Is the "gdata" library (or at least its Spreadsheets API portion) not completely deprecated after all? Is there some third approach that I've missed altogether? I'm working with Java code, to the extent that matters, although I'm sure that any Python API would have a Java equivalent.

like image 962
Steve Perkins Avatar asked Jan 19 '14 14:01

Steve Perkins


People also ask

How do I create a spreadsheet in Google script?

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. // the built-in method SpreadsheetApp. create() is more appropriate.

How do I automatically add data in Google Sheets?

In a Google Sheet, select Add-ons from the main menu, then Supermetrics > Launch. Select a data source from the list of available sources and authorize it to share data with Supermetrics. Build a query using the options available and then click Get Data to Table.

How do I add data to a Google spreadsheet using an app script?

The code uses the appendRow() method of the Sheet object to write a single row of data to the spreadsheet. To append a row, pass an array of values (corresponding to the columns) to the appendRow() method. For example, the code below appends a row containing two values: First name and Last name.


2 Answers

with reference to the new spreadsheet API v4

I have The Best and easy method:

Step 1

Create AsyncTask class, pass the 'GoogleAccountCredential credential' to it.

Step 2

Use the API to create the a new SpreadSheet.

CODE

 private class MakeRequestTask extends AsyncTask<Void, Void, Void> {
    private com.google.api.services.sheets.v4.Sheets mService = null;

    // The constructor
    MakeRequestTask(GoogleAccountCredential credential) {
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        mService = new com.google.api.services.sheets.v4.Sheets.Builder(
                transport, jsonFactory, credential)
                .setApplicationName("Android spreadsheet client")
                .build();
    }                    

    protected void doInBackground(Void... params) {

        // function to create the spreadsheet
        creadSpreadSheet();
    }

    // creates a new spreadsheet
    private void creadSpreadSheet() throws IOException{
        com.google.api.services.sheets.v4.model.Spreadsheet mSpreadsheet, newSpreadSheet;
        mSpreadsheet = new Spreadsheet();
        SpreadsheetProperties spreadsheetProperties = new SpreadsheetProperties();
        spreadsheetProperties.setTitle("Demo SpreadSheet");// name of your spreadsheet
        mSpreadsheet = mSpreadsheet.setProperties(spreadsheetProperties);


        newSpreadSheet = mService.spreadsheets()
                .create(mSpreadsheet)
                .execute();

        // this 'newSpreadsheet' is ready to use for write/read operation.
    }

}

NOTE: Don't forget to put 'SheetsScopes.SPREADSHEETS' scope in the 'credential' in onCreate().

String[] SCOPES = { SheetsScopes.SPREADSHEETS};
    credential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff());
like image 119
Sankar Behera Avatar answered Sep 22 '22 03:09

Sankar Behera


Answer in bullet form ...

  • It's only the old docslist API which is deprecated. The spreadsheet API is still alive and kicking since there is no replacement
  • The gdata libs are possibly no longer supported, but you will probably be better served using the spreadsheet API directly anyway
  • The Drive API is only concerned with operations at the whole file level
  • You could create a populated spreadsheet using the Drive API by uploading a file in a format that can be converted to a Google Spreadsheet, eg. MS Excel
  • Be aware that the spreadsheet API (and possibly the Drive API) do not yet support the new (end 2013) spreadsheet format
like image 22
pinoyyid Avatar answered Sep 20 '22 03:09

pinoyyid