How to programmatically create a Google Docs spreadsheet WITH CONTENT?

后端 未结 3 2045
小蘑菇
小蘑菇 2021-01-05 04:19

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 S

相关标签:
3条回答
  • 2021-01-05 04:39

    Google-apps-script will also create Spreadsheets and let you add data. See https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app It can do more than the GData api, i.e. google-apps-script can set cell fonts/colors etc.

    But depends on your needs, GData is low level in style, and so I find it faster. There is a simple google GData demo CellDemo.java. http://gdata-java-client.googlecode.com/svn-history/r51/trunk/java/sample/spreadsheet/cell/CellDemo.java It show off the Gdata Spreadsheets API features.

    0 讨论(0)
  • 2021-01-05 04:48

    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
    0 讨论(0)
  • 2021-01-05 04:54

    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());
    
    0 讨论(0)
提交回复
热议问题