in src/dfcx_scrapi/tools/datastore_evaluator.py [0:0]
def create_sheet(
self, worksheets, title, parent, chunk_size, sheets_service,
drive_service) -> Union[str, None]:
"""Creates a new spreadsheet with worksheets."""
body = {"properties": {"title": title}}
create_request = sheets_service.spreadsheets().create(
body=body, fields="spreadsheetId"
)
create_result = create_request.execute()
sheet_id = create_result.get("spreadsheetId")
parents_request = drive_service.files().get(
fileId=sheet_id, fields="parents")
parents_result = parents_request.execute()
parents = parents_result.get("parents")
previous_parents = ",".join(parents) if parents else None
if not sheet_id:
return
for worksheet_title, content in worksheets.items():
content_dict = content.to_dict(orient="split")
self.add_worksheet(
sheet_id=sheet_id,
content=[content_dict["columns"]] + content_dict["data"],
title=worksheet_title,
sheets_service=sheets_service,
chunk_size=chunk_size,
)
all_request = sheets_service.spreadsheets().get(spreadsheetId=sheet_id)
all_result = all_request.execute()
default_sheet_id = all_result["sheets"][0]["properties"]["sheetId"]
self.delete_worksheet(sheet_id, default_sheet_id, sheets_service)
_ = drive_service.files().update(
fileId=sheet_id,
addParents=parent,
removeParents=previous_parents,
fields="id, parents"
).execute()
return f"https://docs.google.com/spreadsheets/d/{sheet_id}/edit"