簡體   English   中英

如何通過Android的Drive API讀取Google表格內容?

[英]How do I read Google Sheet content via Android's Drive API?

我目前正在開發一個應用程序,用戶可以從其Google雲端硬盤中選擇一個Google表格文件(.xlxs)。 然后,我的應用程序將提取並處理該工作表的某些內容。

我使用的是專為Android設計的Google Drive API,並利用了Google Drive Android Demos中的示例類

到目前為止,我已經實現了一個Drive文件選擇器,用於顯示用戶的Drive文件。 選擇一個文件將返回該文件的ID。


第一個問題是我似乎無法設置mime類型,因此只能選擇xlsl文件。 Google搜索告訴我

application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

是正確的啞劇類型。

我在這種情況下使用它

IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .setMimeType(new String[]{"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
            .build(getGoogleApiClient());

我以前沒有遇到過mime類型,但是“ text / plain”作為meme類型似乎有效,並且可以在相同的mime類型列表中找到它們。

第二個問題基本上與我應該如何從工作表中的某些列和行中提取字符串有關。

這是處理所選文件內容的代碼(當然可以修改,只是不知道如何)。

protected String doInBackgroundConnected(DriveId... params) {
        String contents = null;
        DriveFile file = params[0].asDriveFile();
        DriveContentsResult driveContentsResult =
                file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return null;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(driveContents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            contents = builder.toString();
        } catch (IOException e) {
            Log.e(TAG, "IOException while reading from the stream", e);
        }

        driveContents.discard(getGoogleApiClient());
        return contents;
    }

因此,我用Google搜索並在社區中轉了一圈,並提出了以下建議來解決您的問題:

第一個問題

由於您只是想編輯表格,為什么不只使用setSelectionFilter呢? 它將選擇器上顯示的文件限制為指定的文件類型。

對於第二個問題:

我認為為了在Google表格中玩轉(更新),您需要在類似的帖子上按照此答案使用Sheets API

“ Google Spreadsheet Api允許在電子表格中進行復雜的操作, 例如按行和列訪問數據 。”

希望這能以某種方式對您有所幫助,至少至少讓您知道接下來要解決的問題。 祝好運。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM