Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Utwórz Google Apps Script, który wysyła żądania do interfejsu Google Sheets API.
Przewodniki Szybki start wyjaśniają, jak skonfigurować i uruchomić aplikację, która wywołuje interfejs API Google Workspace. W tym krótkim wprowadzeniu użyjemy uproszczonego podejścia do uwierzytelniania, które jest odpowiednie w środowisku testowym. W przypadku środowiska produkcyjnego zalecamy zapoznanie się z informacjami o uwierzytelnianiu i autoryzacji przed wybraniem danych logowania odpowiednich dla Twojej aplikacji.
W Apps Script przewodniki Szybki start Google Workspace używają zaawansowanych usług Google do wywoływania interfejsów API Google Workspace i obsługi niektórych szczegółów procesu uwierzytelniania i autoryzacji.
/** * Creates a Sheets API service object and prints the names and majors of * students in a sample spreadsheet: * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get */functionlogNamesAndMajors(){constspreadsheetId='1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms';constrangeName='Class Data!A2:E';try{// Get the values from the spreadsheet using spreadsheetId and range.constvalues=Sheets.Spreadsheets.Values.get(spreadsheetId,rangeName).values;// Print the values from spreadsheet if values are available.if(!values){console.log('No data found.');return;}console.log('Name, Major:');for(constrowinvalues){// Print columns A and E, which correspond to indices 0 and 4.console.log(' - %s, %s',values[row][0],values[row][4]);}}catch(err){// TODO (developer) - Handle Values.get() exception from Sheet APIconsole.log(err.message);}}
Kliknij Zapisz .
Kliknij Projekt bez tytułu, wpisz Szybki start i kliknij Zmień nazwę.
Konfigurowanie skryptu
Włączanie interfejsu Google Sheets API
Otwórz projekt Apps Script.
Kliknij Edytorcode.
Obok opcji Usługi kliknij Dodaj usługę add .
Wybierz
Sheets API
i kliknij Add (Dodaj).
Uruchamianie przykładu
W edytorze Apps Script kliknij Uruchom.
Przy pierwszym uruchomieniu przykładu pojawi się prośba o autoryzację dostępu:
Kliknij Przejrzyj uprawnienia.
Wybierz konto.
Kliknij Zezwól.
Dziennik wykonania skryptu pojawi się u dołu okna.
[null,null,["Ostatnia aktualizacja: 2025-09-04 UTC."],[],[],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Google Sheets 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- A Google Account\n\n\u003c!-- --\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script in the Apps Script editor 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\nsheets/quickstart/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/quickstart/quickstart.gs) \n\n```javascript\n/**\n * Creates a Sheets API service object and prints the names and majors of\n * students in a sample spreadsheet:\n * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit\n * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get\n */\nfunction logNamesAndMajors() {\n const spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms';\n const rangeName = 'Class Data!A2:E';\n try {\n // Get the values from the spreadsheet using spreadsheetId and range.\n const values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;\n // Print the values from spreadsheet if values are available.\n if (!values) {\n console.log('No data found.');\n return;\n }\n console.log('Name, Major:');\n for (const row in values) {\n // Print columns A and E, which correspond to indices 0 and 4.\n console.log(' - %s, %s', values[row][0], values[row][4]);\n }\n } catch (err) {\n // TODO (developer) - Handle Values.get() exception from Sheet API\n console.log(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 Sheets API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Sheets 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- [Troubleshoot authentication and authorization issues](/workspace/sheets/api/troubleshoot-authentication-authorization)\n- [Sheets API reference documentation](/workspace/sheets/api/reference/rest)"]]