如何使用Android Google Spreadsheet API创建列表范围验证

时间:2014-12-05 12:38:43

标签: java android google-sheets google-spreadsheet-api google-drive-api

我正在创建一个Android应用程序,在Google云端硬盘中创建一个新的电子表格并在其中插入一些数据。目前我可以插入免费的文本和公式,但我需要添加一些特定的验证,特别是一个带有预定义值的下拉列表。

预期结果应该是这样的:Dropdown Image

我正在使用此代码:

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;
           }
       }

我需要一种方法来以编程方式从API中插入此条件范围列表​​,而不是从UI。

谢谢。

2 个答案:

答案 0 :(得分:2)

requireValueInList(values,showDropdown)

您可以使用 Google Apps脚本来执行此操作,您可以从服务器调用google apps脚本,但无法使用(gdata)电子表格api执行此操作。

来自文档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);

答案 1 :(得分:0)

Apps脚本主要用于Google Apps之上的工作流程自动化。并没有简单的方法如何将它与客户连接,但有一种方法。取决于您的要求以及谁将使用该应用程序。

我对Android与Google Spreadsheets的整合更感兴趣。我期待Drive API可以处理Google Spreadsheets的创建和修改。但似乎Google Spreadsheets API是唯一的出路。