Why use googlesheets4 and googledrive together?googlesheets4 wraps the Sheets API v4, which lets you read, write, and format data in Sheets. The Sheets API is very focused on spreadsheet-oriented data and metadata, i.e. (work)sheets and cells. Show
The Sheets API offers practically no support for file-level operations, other than basic spreadsheet creation. There is no way to delete, copy, or rename a Sheet or to place it in a folder or to change its sharing permissions. We must use the Drive API for all of this, which is wrapped by the googledrive package (https://googledrive.tidyverse.org). Another reason to use the googlesheets4 and googledrive packages together is for ease of file (Sheet) identification. The googlesheets4 package requires you to specify the target Sheet by its ID, not by its name. That’s because the underlying APIs only accept file IDs. But the googledrive package offers lots of support for navigating between human-friendly file names and their associated IDs. This support applies to all files on Drive and, specifically, to Sheets. Therefore, it is common to use googledrive and googlesheets4 together in a script or app. Coordinating authHow does auth work if you’re using googlesheets4 and googledrive? The path of least resistance is to do nothing and just let each package deal with its own auth. This works fine! But it’s a bit clunky and you need to make sure you’re using the same Google identity (email) with each package/API. It can be nicer to be proactive about auth and use the same token for your googledrive and googlesheets4 work. Below we show a couple of ways to do this. Auth with googledrive first, then googlesheets4Outline:
First attach both packages. Do auth first with googledrive. Remember Now you can use googledrive functions, like
If you ever want to confirm the currently authenticated user, both packages provide a
We are using a service account to render this article. But if you’ve used the default OAuth flow, this should correspond to the email of the Google account you logged in with. Auth with googlesheets4 first, then googledriveOutline:
First attach both packages. Do auth first with googlesheets4, specifying a Drive scope. Remember Now you can use googledrive functions to list files or find them by name, path, or other property. Then, once you’ve identified the target file, use googlesheets4 to do spreadsheet-specific tasks.
Scope savvyIf you only need “read” access to Drive or Sheets, the conservative thing to do is to specify a read-only scope. This is a great way to limit the damage anyone can do with the token – you or someone else – through carelessness or malice. If you are storing a token on a remote or shared location, it is wise to use the most conservative scope that still gets the job done. Here are various scopes relevant to googledrive and googlesheets4 and what they would allow.
If you are just using Drive to identify Sheets and are only reading from those Sheets, the
If you are not using Drive at all, i.e. you always identify Sheets by file ID, and
you are only reading from those Sheets, you only need googlesheets4 and
How do I add a Google sheet to a folder?Manually move to a folder. On your computer, go to drive.google.com.. Right-click the item you want to move.. Click Move to.. Select or create a folder.. Click Move.. How do you create a new sheet in Google Sheets?To create a new spreadsheet:. Open the Sheets home screen at sheets.google.com.. Click New. . This will create and open your new spreadsheet.. Can you organize Google Sheets in folders?Create a Folder
On the left Navigation Bar, click Browse. In the left panel, right-click the location where you'd like to add the new folder—the Sheets directory, another folder, or a workspace— and select Create New > Folder. Enter a folder name and then click OK.
How do I get Google Sheets to automatically add?Use autofill to complete a series. On your computer, open a spreadsheet in Google Sheets.. In a column or row, enter text, numbers, or dates in at least two cells next to each other.. Highlight the cells. You'll see a small blue box in the lower right corner.. Drag the blue box any number of cells down or across.. |