How do I connect Android apps with Google Sheets spreadsheets?

后端 未结 4 823
难免孤独
难免孤独 2020-12-14 12:08

I\'m trying to do an Android app that needs to work with Google spreadsheet API. I\'m new in this, so I\'m starting with the version 3 of the api: https://developers.google.

4条回答
  •  佛祖请我去吃肉
    2020-12-14 13:02

    (Feb 2017) The question (and most answers) are now out-of-date as:

    1. GData APIs are the previous generation of Google APIs. While not all GData APIs have been deprecated, all modern Google APIs do not use the Google Data protocol
    2. Google released a new Google Sheets API (v4; not GData) in 2016, and
    3. Android Studio is now the preferred IDE over Eclipse. In order to use Google APIs, you need to get the Google APIs Client Library for Android (or for more general Java, the Google APIs Client Library for Java). Now you're set.

    To start, the latest Sheets API is much more powerful than all older versions. The latest API provides features not available in older releases, namely giving developers programmatic access to a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resize rows/columns, add pivot tables, create charts, etc.).

    That said, yeah, it's tough when there aren't enough good (working) examples floating around, right? In the official docs, we try to put "quickstart" examples in as many languages as possible to help get you going. In that spirit, here are the Android quickstart code sample as well as the more general Java Quickstart code sample. For convenience, here's the Sheets API JavaDocs reference.

    Another answer suggested using OAuth2 for data authorization, which you can do with this auth snippet from the quickstart above, plus the right scope:

    // Sheets RO scope
    private static final String[] SCOPES = {SheetsScopes.SPREADSHEETS_READONLY};
        :
    
    // Initialize credentials and service object
    mCredential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff());
    

    If you're not "allergic" to Python, I've made several videos with more "real-world" examples using the Sheets API (non-mobile though):

    • Migrating SQL data to a Sheet (code deep dive post)
    • Formatting text using the Sheets API (code deep dive post)
    • Generating slides from spreadsheet data (code deep dive post)

    Finally, note that the Sheets API performs document-oriented functionality as described above. For file-level access, i.e. import, export etc. you'd use the Google Drive API instead; specifically for mobile, use the Google Drive Android API. Hope this helps!

提交回复
热议问题