1

I'm trying to create a spreadsheet via my android app. So far I've been only successful in creating an empty text document. Google's Spreadsheet API instructs me to follow Google Documents List API in order to create a spreadsheet document in Google Drive. In Google Documents List API it says:

To create a new, empty spreadsheet, follow the instructions in Creating a new document or file with metadata only. When doing so, use a category term of http://schemas.google.com/docs/2007#spreadsheet.

In the link above I've found the next .NET code:

using System;
using Google.GData.Client;
using Google.GData.Documents;

namespace MyDocumentsListIntegration
{
  class Program
  {
    static void Main(string[] args)
    {
      DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");

      // TODO: Authorize the service object for a specific user (see Authorizing requests)

      // Instantiate a DocumentEntry object to be inserted.
      DocumentEntry entry = new DocumentEntry();

      // Set the document title
      entry.Title.Text = "Legal Contract";

      // Add the document category
      entry.Categories.Add(DocumentEntry.DOCUMENT_CATEGORY);

      // Make a request to the API and create the document.
      DocumentEntry newEntry = service.Insert(
          DocumentsListQuery.documentsBaseUri, entry);
    }
  }
}

I've used this code to try and create a spreadsheet, but only the third variant worked (using DocsService, without adding Category and using feedUrl URL object).

Here's part of my working code (upload() is being called when user clicks a button):

private void upload() {
        SpreadSheetAsyncTask sprdSheetAsyncTsk = new SpreadSheetAsyncTask();

        sprdSheetAsyncTsk.execute();

    }

    public class SpreadSheetAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            spreadSheetService = new SpreadsheetService("Salary_Calculator");
            docService = new DocsService("Salary_Calculator");
            try {

                spreadSheetService.setUserCredentials(USERNAME, PASSWORD);
                docService.setUserCredentials(USERNAME, PASSWORD);

                URL feedUrl = new URL(
                        "https://docs.google.com/feeds/default/private/full/");
                URL tmpFeedUrl = new URL(
                        "https://spreadsheets.google.com/feeds/worksheets/key/private/full");

                DocumentEntry entry = new DocumentEntry();

                Calendar timeRightNow = GregorianCalendar.getInstance();

                entry.setTitle(new PlainTextConstruct(
                        "Salary Calculator Spreadsheet "
                                + timeRightNow.get(Calendar.DAY_OF_MONTH) + "/"
                                + (timeRightNow.get(Calendar.MONTH) + 1) + "/"
                                + timeRightNow.get(Calendar.YEAR)));

//              Category object = new Category("spreadsheet",
//                      "http://schemas.google.com/docs/2007#spreadsheet");
//              
//
//              entry.getCategories().add(object);
                
                /*
                 * TODO TEST AREA
                 */

                entry = docService.insert(feedUrl, entry);
                /*
                 * TODO TEST AREA
                 */
            } catch (AuthenticationException e) {
                cancel(true);
                loginDialog("Wrong username or password");
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (ServiceException e) {
                cancel(true);
                loginDialog("Wrong username or password");
            } catch (Exception e) {
                loginDialog("Wrong username or password");
            }

            return null;
        }
    }
                

The non working code uses the category object object (as shown in the code comment) above and uses entry = spreadSheetService.insert(feedUrl, entry);

My question is - what did they want me to do when they wrote use a category term of http://schemas.google.com/docs/2007#spreadsheet?

4

0 回答 0