How to create a list range validation using the Android Google Spreadsheet API

喜夏-厌秋 提交于 2019-12-22 13:08:09

问题


I am creating an android application that creates a new Spreadsheet in Google Drive and inserts some data inside. Currently I can insert free Text and Formulas, but I need to add some specific validations and specifically a drop down list with predefined values.

The expected result should be something like this:

I am using this code:

SpreadsheetFeed feed = spreadsheetService.getFeed(
                FeedURLFactory.getDefault()
                        .getSpreadsheetsFeedUrl(),
                SpreadsheetFeed.class); 
        // Creating the list of spreasheets in GDrive
        List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();

            // parsing trough the feed entries
        for (int i = 0; i < spreadsheets.size(); i++) {
            com.google.gdata.data.spreadsheet.SpreadsheetEntry e = (com.google.gdata.data.spreadsheet.SpreadsheetEntry) spreadsheets.get(i);
            // IF WE LOCATE THE FILE BASED ON THE FILENAME
            if( e.getTitle().getPlainText().equals(Constants.FILE_PREFIX + mFileName)) {

                Log.d(TAG, "ENTRY: " + e.getTitle().getPlainText());

                URL worksheetFeedUrl = e.getWorksheetFeedUrl();

                Log.d(TAG, "worksheetFeedUrl: " + worksheetFeedUrl);

                // The first time this feed is used to create the new worksheet
                WorksheetFeed worksheetFeed = spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);

                Log.d(TAG, "worksheetFeed OK !");

                // Create the second worksheet 
                WorksheetEntry newWorksheet = new WorksheetEntry(15, 5);
                newWorksheet.setTitle(new PlainTextConstruct("Sheet2"));
                worksheetFeed.insert(newWorksheet);

                // The second time this feed is used to get the worksheets
                worksheetFeed = spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);

                // Get the first worksheet and insert the titles
                List <WorksheetEntry> worksheetEntrys = worksheetFeed.getEntries ();
                WorksheetEntry sheet1 = worksheetEntrys.get(0);
                WorksheetEntry sheet2 = worksheetEntrys.get(1);

                URL sheet1CellFeedUrl = sheet1.getCellFeedUrl ();
                CellFeed sheet1CellFeed = spreadsheetService.getFeed (sheet1CellFeedUrl, CellFeed.class);

                sheet1CellFeed.insert (new CellEntry (1, 1, getResources().getString(R.string.cell_title_name)));
                sheet1CellFeed.insert (new CellEntry (1, 2, getResources().getString(R.string.cell_title_description)));
                sheet1CellFeed.insert (new CellEntry (3, 2, getResources().getString(R.string.some_string)));                   
                sheet1CellFeed.insert (new CellEntry (13, 2, "=COUNTIF(Sheet1!F2:F,B3)"));
                sheet1CellFeed.insert (new CellEntry (14, 2, "=B9 - TODAY()"));

                break;
           }
       }

I need a way to insert this Criteria Range List From the API programmatically and not from the UI.

Thank you.


回答1:


requireValueInList(values, showDropdown)

You can do it with Google Apps Script, you can call google apps script from a server, but there is no way to do it with the (gdata) spreadsheet-api.

From docs requireValueInList

 // Set the data validation for cell A1 to require "Yes" or "No", with no dropdown menu.
 var cell = SpreadsheetApp.getActive().getRange('A1');
 var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Yes', 'No'], false).build();
 cell.setDataValidation(rule);



回答2:


Apps Script is primarily designed for workflow automation on top of Google Apps. And there is no easy way how to connect it with clients but there is a way. Depends what are your requirements and who will use the app.

I am more interested in Android integration with Google Spreadsheets. I was expecting that Drive API can handle creation and modification of Google Spreadsheets. But it seems the Google Spreadsheets API is the only way to go.



来源:https://stackoverflow.com/questions/27316098/how-to-create-a-list-range-validation-using-the-android-google-spreadsheet-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!