11

I am able to create a sheet with the code below in the root of ‘My Drive’ but how do I create the sheet under a folder in “My Drive” or “Shared drives”?

from googleapiclient.discovery import build

service = build(‘sheets’, ‘v4’, credentials=creds)
sheet = service.spreadsheets()
body = {}
results = sheet.create(body=body).execute()
pprint(results)
Tanaike
  • 181,128
  • 11
  • 97
  • 165
bayman
  • 1,579
  • 5
  • 23
  • 46

1 Answers1

21
  • You want to create new Spreadsheet in the specific folder.
  • You want to achieve this using google-api-python-client with python.

If my understanding is correct, how about this answer?

Issue:

Unfortunately, in the current stage, new Spreadsheet cannot be directly created to the specific folder of Google Drive using Sheets API. In this case, Drive API is required to be used.

Sample script:

Before you run the script, please set the folder ID.

Pattern 1:

In this pattern, the new Spreadsheet is directly created to the specific folder in your Google Drive. In order to create Spreadsheet, the mimeType of application/vnd.google-apps.spreadsheet is used.

Script:
drive = build('drive', 'v3', credentials=creds)
file_metadata = {
    'name': 'sampleName',
    'parents': ['### folderId ###'],
    'mimeType': 'application/vnd.google-apps.spreadsheet',
}
res = drive.files().create(body=file_metadata).execute()
print(res)

Pattern 2:

In this pattern, after the new Spreadsheet is created by Sheets API, the Spreadsheet is moved to the specific folder in your Google Drive.

Script:
# Create Spreadsheet to the root folder.
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
body = {}
results = sheet.create(body=body).execute()
pprint(results)

# Move the created Spreadsheet to the specific folder.
drive = build('drive', 'v3', credentials=creds)
folderId = '### folderId ###'
res = drive.files().update(fileId=results['spreadsheetId'], addParents=folderId, removeParents='root').execute()
print(res)

Note:

  • For both samples, please add a scope of https://www.googleapis.com/auth/drive. And when the scopes are added, please remove the created credential file including the refresh token and authorize again. By this, the additional scopes are reflected to the refresh token.
  • If you want to use the shared Drive, please modify as follows.
    • For pattern 1
      • file_metadata = {'name': 'sampleName','parents': ['### folderId ###'],'mimeType': 'application/vnd.google-apps.spreadsheet','driveId': "###"}
      • res = drive.files().create(body=file_metadata, supportsAllDrives=True).execute()
    • For pattern 2
      • res = drive.files().update(fileId=results['spreadsheetId'], body={'driveId': "###"}, addParents=folderId, removeParents='root', supportsAllDrives=True).execute()

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @bayman Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Sep 26 '19 at 22:12
  • If I'm using `body = {"properties": {"title": "title_here"}, "sheets": sheetsObj} res = sheets.spreadsheets().create(body=body).execute() print(res)`, where do I insert the argument for folder ID? –  Jan 27 '20 at 15:39
  • Thanks for the answer! Just have to comment that pattern 1 works for shared folders without setting "driveId". The supportsAllDrives: true is sufficient – rubenhak Oct 31 '21 at 00:31