Using the Python example taken directly from the "Google Sheets API documentation > Create a spreadsheet" and changing only the credentials, I am experiencing poor performance. It's taking ~5 seconds to create an empty spreadsheet.
This is my code with credentials redacted:
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2.credentials import Credentials
def create(title):
creds = Credentials(
token="xxx",
refresh_token="xxx",
token_uri="https://oauth2.googleapis.com/token",
client_id="xxx",
client_secret="xxx",
)
try:
service = build("sheets", "v4", credentials=creds)
spreadsheet = {"properties": {"title": title}}
import time
start_time = time.time()
spreadsheet = (
service.spreadsheets()
.create(body=spreadsheet, fields="spreadsheetId")
.execute()
)
print(f"Time taken: {time.time() - start_time} seconds")
print(f"Spreadsheet ID: {(spreadsheet.get('spreadsheetId'))}")
return spreadsheet.get("spreadsheetId")
except HttpError as error:
print(f"An error occurred: {error}")
return error
if __name__ == "__main__":
# Pass: title
import time
start_time = time.time()
create("mysheet1")
print(f"Time taken: {time.time() - start_time} seconds")
Output:
Time taken: 4.405630826950073 seconds
Spreadsheet ID: xxx
Time taken: 4.407716989517212 seconds
I've looked elsewhere and I can only find solutions to optimise big spreadsheets but this is empty.
- Why is this?
- Is it expected?
- Can I improve it?
Asking in the hope that Google reps will respond here.
credentialsofoauth2have you tried usingservice account?