Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Erstellen Sie ein Google Apps Script, mit dem Anfragen an die Google Calendar API gesendet werden.
In Kurzanleitungen wird beschrieben, wie Sie eine App einrichten und ausführen, die eine Google Workspace API aufruft. In dieser Kurzanleitung wird ein vereinfachtes Authentifizierungsverfahren verwendet, das für eine Testumgebung geeignet ist. Für eine Produktionsumgebung empfehlen wir, sich mit Authentifizierung und Autorisierung vertraut zu machen, bevor Sie die für Ihre App geeigneten Zugangsdaten auswählen.
In Apps Script verwenden Google Workspace-Schnellstarts erweiterte Google-Dienste, um Google Workspace APIs aufzurufen und einige Details des Authentifizierungs- und Autorisierungsablaufs zu verarbeiten.
Ziele
Konfigurieren Sie die Umgebung.
Erstellen und konfigurieren Sie das Skript.
Führen Sie das Skript aus.
Vorbereitung
Ein Google-Konto, für das Google Kalender aktiviert ist.
/** * Lists 10 upcoming events in the user's calendar. * @see https://developers.google.com/calendar/api/v3/reference/events/list */functionlistUpcomingEvents(){constcalendarId='primary';// Add query parameters in optionalArgsconstoptionalArgs={timeMin:(newDate()).toISOString(),showDeleted:false,singleEvents:true,maxResults:10,orderBy:'startTime'// use other optional query parameter here as needed.};try{// call Events.list method to list the calendar events using calendarId optional query parameterconstresponse=Calendar.Events.list(calendarId,optionalArgs);constevents=response.items;if(events.length===0){console.log('No upcoming events found');return;}// Print the calendar eventsfor(consteventofevents){letwhen=event.start.dateTime;if(!when){when=event.start.date;}console.log('%s (%s)',event.summary,when);}}catch(err){// TODO (developer) - Handle exception from Calendar APIconsole.log('Failed with error %s',err.message);}}
Klicken Sie auf „Speichern“ .
Klicken Sie auf Unbenanntes Projekt, geben Sie Schnellstart ein und klicken Sie auf Umbenennen.
Skript konfigurieren
Google Calendar API aktivieren
Öffnen Sie das Apps Script-Projekt.
Klicken Sie auf Editorcode.
Klicken Sie neben Dienste auf „Dienst hinzufügen“ add .
Wählen Sie die Calendar API aus und klicken Sie auf Hinzufügen.
Beispiel ausführen
Klicken Sie im Apps Script-Editor auf Ausführen.
Wenn Sie das Beispiel zum ersten Mal ausführen, werden Sie aufgefordert, den Zugriff zu autorisieren:
Klicken Sie auf Berechtigungen ansehen.
Wählen Sie ein Konto aus.
Klicken Sie auf Zulassen.
Das Ausführungsprotokoll des Skripts wird unten im Fenster angezeigt.
[null,null,["Zuletzt aktualisiert: 2025-08-29 (UTC)."],[],[],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Google Calendar API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n\u003cbr /\u003e\n\n- A Google account with Google Calendar enabled.\n\n\u003cbr /\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\ncalendar/quickstart/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/calendar/quickstart/quickstart.gs) \n\n```javascript\n/**\n * Lists 10 upcoming events in the user's calendar.\n * @see https://developers.google.com/calendar/api/v3/reference/events/list\n */\nfunction listUpcomingEvents() {\n const calendarId = 'primary';\n // Add query parameters in optionalArgs\n const optionalArgs = {\n timeMin: (new Date()).toISOString(),\n showDeleted: false,\n singleEvents: true,\n maxResults: 10,\n orderBy: 'startTime'\n // use other optional query parameter here as needed.\n };\n try {\n // call Events.list method to list the calendar events using calendarId optional query parameter\n const response = Calendar.Events.list(calendarId, optionalArgs);\n const events = response.items;\n if (events.length === 0) {\n console.log('No upcoming events found');\n return;\n }\n // Print the calendar events\n for (const event of events) {\n let when = event.start.dateTime;\n if (!when) {\n when = event.start.date;\n }\n console.log('%s (%s)', event.summary, when);\n }\n } catch (err) {\n // TODO (developer) - Handle exception from Calendar API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nEnable the Google Calendar API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Calendar API and click **Add**.\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [Try the Google Workspace APIs in the APIs explorer](/workspace/explore)\n- [Create events](/workspace/calendar/create-events)\n- [Troubleshoot authentication and authorization issues](/workspace/calendar/api/troubleshoot-authentication-authorization)\n- [Calendar API reference documentation](/workspace/calendar/v3/reference)"]]