במדריכים למתחילים של Google Workspace נעשה שימוש בספריות הלקוח של ה-API כדי לטפל בחלק מהפרטים של תהליך האימות וההרשאה. מומלץ להשתמש בספריות הלקוח באפליקציות שלכם. במדריך למתחילים הזה נעשה שימוש בגישה פשוטה לאימות שמתאימה לסביבת בדיקה. בסביבת ייצור, מומלץ ללמוד על אימות והרשאה לפני שבוחרים את פרטי הכניסה שמתאימים לאפליקציה.
/** * 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='ClassData!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('Nodatafound.');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);}}
לוחצים על סמל השמירה .
לוחצים על Untitled project, מקלידים Quickstart ולוחצים על Rename.
הגדרת הסקריפט
הפעלת Google Sheets API
פותחים את הפרויקט ב-Apps Script.
לוחצים על עריכהcode.
לצד Services (שירותים), לוחצים על Add a service (הוספת שירות)
add .
בוחרים ב-Sheets API ולוחצים על הוספה.
הרצת הדוגמה
עורכים את הסקריפט ב-Apps Script ולוחצים על הפעלה.
בפעם הראשונה שתפעילו את הדוגמה, תתבקשו לאשר את הגישה:
[null,null,["עדכון אחרון: 2024-12-21 (שעון UTC)."],[[["This quickstart demonstrates how to use Google Apps Script to connect to and retrieve data from the Google Sheets API."],["You will need a Google Account and access to Google Drive to complete the setup."],["The process involves creating a script, enabling the Google Sheets API, and authorizing access for the script to interact with your spreadsheets."],["Upon successful execution, the script will print data from a sample spreadsheet to the execution log."],["For production environments, further exploration of authentication, authorization, and credential management is recommended."]]],["This document outlines setting up and running a Google Apps Script that interacts with the Google Sheets API. Key actions include: configuring the environment, creating a script, enabling the Sheets API service, and running the script. The script accesses a sample spreadsheet, retrieves student names and majors, and logs them. Authentication authorization is required initially. Client libraries for API are recommended. It also provides links for advanced learning, troubleshooting and bug reporting.\n"]]