1

I pick Google Spreadsheet with Intent.ACTION_OPEN_DOCUMENT and can get it's Virtual File URI. But to use Google Sheets API, I need Spreadsheet ID, and it seems Virtual File URI doesn't contain it. How can I get Google Spreadsheet ID from data, received by Intent.ACTION_OPEN_DOCUMENT?

My simplified code snippet:

public class MainActivity extends AppCompatActivity {
    ...
    public void onMenuFile(MenuItem item) {
        // called when 'File' menu item is selected
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("*/*");
        startActivityForResult(intent, EDIT_REQUEST_CODE);
    }
    ...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode != EDIT_REQUEST_CODE || resultCode != Activity.RESULT_OK || data == null) {
            return;
        }
        uri = data.getData();
        if (isVirtualFile(uri)) {
           // here i would like to get spreadsheetID somehow, and use it:
           ValueRange response = service.spreadsheets().values()
               .get(spreadsheetID, sheetTitle)
               .execute();
           List<List<Object>> values = response.getValues();
           ...and so on...
        }
        ...
    }
    ...

    private boolean isVirtualFile(URI uri)) {
       here code from
       https://developer.android.com/training/data-storage/shared/documents-files#java
    }
}

Further researches show that Intent.ACTION_VIEW successfully find required spreadsheet by virtual file URI:

if (isVirtualFile(uri)) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
   startActivity(intent);
} }

and actual activity class is com.google.android.apps.docs.app.OpenSafUrlActivity.

It would be nice to look into the code, but I could not find sources...

UPDATE

It seems that i've found a workaround here: Find a Google Drive Sheet By Name Using API 4 In Android

Eduard
  • 91
  • 2
  • 4

1 Answers1

0

SpreadsheetID needs to be hard coded in. See this google code snippet:

https://github.com/googleworkspace/java-samples/blob/master/sheets/snippets/src/main/java/SpreadsheetSnippets.java#L18-L30

You can only call getData() after the spread sheet object has been created.

To find out what the spreadhsheet id go to the desired sheet on google drive and look in the URL:

https://docs.google.com/spreadsheets/d/**spreadsheetId**/edit#gid=0

More info here: https://developers.google.com/sheets/api/guides/concepts

219CID
  • 340
  • 5
  • 15
  • But I don't need to create new spreadsheet. It already exists on Google Drive as "virtual file". I just want to select it with Intent.ACTION_OPEN_DOCUMENT, and then read/update it using Sheets API. – Eduard Oct 20 '20 at 05:37
  • "...To find out what the spreadhsheet id go to the desired sheet on google drive and look in the URL..." no, it's not the case. I want to use arbitrary google spreadsheet (on my google drive) picking it by ACTION_OPEN_DOCUMENT intent. And at that moment I can't see any spreadsheet URL or spreadsheetID itself. – Eduard Oct 21 '20 at 06:25