如何在Web应用的Google Drive Picker中添加「Recent」标签页?
Hey there! I totally get what you're aiming for—you want that familiar "Recent" tab from Google Docs, which shows all files you've recently accessed, not just the ones you picked via the Picker tool (which is what the built-in "Recently Picked" feature does).
The catch is that the Picker API doesn't have a direct pre-built view for this, but we can work around it using a custom query with the DocsView class, leveraging the same underlying logic Google Docs uses.
Here's how to implement it:
The "Recent" view in Google Docs pulls files sorted by the viewedByMeTime timestamp. We can replicate this by creating a DocsView and setting a custom query that filters for files you've viewed, then sorts them by the most recent first.
Modify your existing code to add this new view:
new google.picker.PickerBuilder() .enableFeature(google.picker.Feature.MULTISELECT_ENABLED) .enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES) .setAppId(appId) .setOAuthToken(oauthToken) // Add the new "Recent" view (position it wherever you want in the tab list) .addView(new google.picker.DocsView() .setQuery('viewedByMeTime > "2000-01-01T00:00:00Z"') // Captures all files you've ever viewed .setSortOrder(google.picker.SortOrder.LAST_VIEWED) // Sorts by most recent view first .setLabel('Recent') // Sets the tab display name .setIncludeFolders(true)) // Optional: include recently viewed folders if needed .addView(new google.picker.DocsView().setIncludeFolders(true).setOwnedByMe(true)) .addView(new google.picker.DocsView().setIncludeFolders(true).setOwnedByMe(false)) .addView(new google.picker.DocsView().setStarred(true).setLabel('Starred')) // Don't forget to add your callback and build method! .setCallback(pickerCallback) .build()
Key Notes:
- The query
viewedByMeTime > "2000-01-01T00:00:00Z"is a broad filter that grabs all files you've ever viewed. If you want to limit it to a shorter window (like the last 30 days), you can calculate a dynamic timestamp instead of using the fixed 2000 date. setSortOrder(google.picker.SortOrder.LAST_VIEWED)ensures the files appear in the same "most recent first" order as Google Docs' native Recent tab.- Double-check that your OAuth token includes the necessary scope—
https://www.googleapis.com/auth/drive.readonlyis sufficient for accessing viewed files, but adjust if your app needs broader permissions.
This should give you the exact "Recent" tab experience you're looking for!
内容的提问来源于stack exchange,提问作者dakairus

