Google Apps 脚本提供了 30 多项内置服务,可用于与用户数据、其他 Google 系统和外部系统进行交互。这些服务以类似于 JavaScript 标准 Math 对象的全局对象形式提供。例如,正如 Math 提供 random() 等方法和 PI 等常量一样,Apps 脚本的电子表格服务提供 openById(id) 等方法、Range 等类(子对象)和 DataValidationCriteria 等枚举。
用于控制 Google Workspace 产品的服务的参考文档收集在本网站边栏的“参考”标题下的“Google Workspace 服务”部分中。实用程序服务(用于创建用户界面、解析 XML 或写入日志数据等)收集在“脚本服务”部分中。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eGoogle Apps Script offers 30+ built-in services for interacting with Google and external systems, accessible as global objects similar to JavaScript's \u003ccode\u003eMath\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eApps Script supports modern ECMAScript syntax and features with the V8 runtime, which is recommended over the older Rhino runtime.\u003c/p\u003e\n"],["\u003cp\u003eServices provide global objects like \u003ccode\u003eGmailApp\u003c/code\u003e or \u003ccode\u003eSpreadsheetApp\u003c/code\u003e to access methods for interacting with specific Google products.\u003c/p\u003e\n"],["\u003cp\u003eChild classes like \u003ccode\u003eRange\u003c/code\u003e or \u003ccode\u003eDocument\u003c/code\u003e are accessed by calling methods of their parent global objects, enabling chained method calls for efficiency.\u003c/p\u003e\n"],["\u003cp\u003eEnums, such as \u003ccode\u003eDriveApp.Access\u003c/code\u003e, provide named values for specific functionalities within services like Drive.\u003c/p\u003e\n"]]],[],null,["# Built-in Google Services\n\nGoogle Apps Script provides more than 30 built-in services for interacting with\nuser data, other Google systems, and external systems. These services are\nprovided as global objects akin to JavaScript's standard\n[`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math)\nobject. For example, just as `Math` offers methods like `random()` and\nconstants like `PI`, Apps Script's\n[Spreadsheet service](/apps-script/reference/spreadsheet) offers methods like\n[`openById(id)`](/apps-script/reference/spreadsheet/spreadsheet-app#openById(String)),\nclasses (child objects) like\n[`Range`](/apps-script/reference/spreadsheet/range), and enums like\n[`DataValidationCriteria`](/apps-script/reference/spreadsheet/data-validation-criteria).\n\nThe reference documentation for services that control\nGoogle Workspace products are collected in the\n\"Google Workspace Services\" section under the\n\"Reference\" header in the sidebar of this site. Utility services (for things\nlike creating user interfaces, parsing XML, or writing log data) are collected\nin the \"Script Services\" section.\n\nModern JavaScript features\n--------------------------\n\nApps Script supports two JavaScript runtimes: the modern\n[**V8**](https://v8.dev/) runtime and an older one powered by Mozilla's\n[**Rhino JavaScript interpreter**](https://en.wikipedia.org/wiki/Rhino_(JavaScript_engine)).\n\nThe [V8 runtime](/apps-script/guides/v8-runtime) supports modern\n[ECMAScript](https://en.wikipedia.org/wiki/ECMAScript) syntax and features.\nThe Rhino runtime is based on the older\n[JavaScript 1.6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.6)\nstandard, plus a few features from\n[1.7](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7) and\n[1.8](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.8).\nYou can [freely choose which runtime](/apps-script/guides/v8-runtime#enabling_the_v8_runtime)\nto use with your script, but the V8 runtime is strongly recommended.\n\nEach runtime supports JavaScript classes and objects that are available to your\nscript in addition to the built-in\nand [advanced Google services](/apps-script/guides/services/advanced). Your\nscripts can use common objects like\n[`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array),\n[`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date),\n[`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp),\n[and so forth](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference),\nas well as the\n[`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) and\n[`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\nglobal objects.\n| **Note:** Because Apps Script code runs on Google's servers (with the exception of [HTML-service](/apps-script/guides/html) pages), browser-based JavaScript features like DOM manipulation or the [`Window`](https://developer.mozilla.org/en-US/docs/Web/API/Window) API are not available in Apps Script.\n\nUsing autocomplete\n------------------\n\nThe script editor provides a \"content assist\" feature, more commonly called\n\"autocomplete,\" which reveals the global objects as well as methods and enums\nthat are valid in the script's current context. Autocomplete suggestions appear\nautomatically whenever you type a period after a global object, enum, or method\ncall that returns an Apps Script class. For example:\n\n- If you type the full name of a global object or select one from autocomplete, then type `.` (a period), you will see all methods and enums for that class.\n- If you type a few characters, you'll see all valid suggestions that begin with those characters.\n\nUnderstanding global objects\n----------------------------\n\nEach service provides at least one global (top-level) object; for example,\nthe [Gmail service](/apps-script/reference/gmail) is accessed solely from\nthe [`GmailApp`](/apps-script/reference/gmail/gmail-app) object. Some services\nprovide multiple global objects; for example, the\n[Base service](/apps-script/reference/base) includes four global objects:\n[`Browser`](/apps-script/reference/base/browser),\n[`Logger`](/apps-script/reference/base/logger),\n[`MimeType`](/apps-script/reference/base/mime-type), and\n[`Session`](/apps-script/reference/base/session).\n\nCalling methods\n---------------\n\nThe global objects of nearly all built-in or\n[advanced services](/apps-script/guides/services/advanced) include methods that\nreturn data or an Apps Script class. Scripts make method calls in this format: \n\n GlobalObjectName.methodName(argument1, argument2, ..., argumentN);\n\nFor example, a script can send an email by calling the\n[`sendEmail(recipient, subject, body)`](/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String))\nmethod of the Gmail service like so: \n\n GmailApp.sendEmail('claire@example.com', 'Subject line', 'This is the body.');\n\nIf a method returns another Apps Script class, you can chain method calls on one\nline. (Return types are shown both in autocomplete and in a method's reference\ndocumentation.) For example, the method\n[`DocumentApp.create()`](/apps-script/reference/document/document-app#create(String))\nreturns a [`Document`](/apps-script/reference/document/document); thus, the\nfollowing two sections of code are equivalent: \n\n var doc = DocumentApp.create('New document');\n var body = doc.getTab('t.0').asDocumentTab().getBody();\n body.appendParagraph('New paragraph.');\n\n // Same result as above.\n DocumentApp.create('New document').getTab('t.0').asDocumentTab().getBody()\n .appendParagraph('New paragraph.');\n\nAccessing child classes\n-----------------------\n\nEvery service includes one or more child classes that cannot be accessed from\nthe top level as a global object can. You cannot use the `new` keyword to\nconstruct these classes, as you can with standard JavaScript classes like\n[`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date);\nyou can only access a child class by calling a method that returns it. If you're\nnot sure how to access a certain class, visit the root page for the service's\nreference documentation and look for a method that returns the class you want.\n\nDealing with interfaces\n-----------------------\n\nA handful of services include special classes that are labeled as \"interfaces\"\nin the reference documentation. These are generic classes used as return types\nfor methods that cannot determine the precise type in advance; for example,\nthe [Document service](/apps-script/reference/document) method\n[`Body.getChild(childIndex)`](/apps-script/reference/document/body#getChild(Integer))\nreturns a generic [`Element`](/apps-script/reference/document/element) object.\n`Element` is an interface that represents some other class, possibly a\n[`Paragraph`](/apps-script/reference/document/paragraph) or\n[`Table`](/apps-script/reference/document/table). Interface objects are rarely\nuseful on their own; instead, you usually want to call a method like\n[`Element.asParagraph()`](/apps-script/reference/document/element#asParagraph())\nto cast the object back to a precise class.\n\nWorking with enums\n------------------\n\nMost services include a few enums (enumerated types) of named values. For\nexample, the [Drive service](/apps-script/reference/drive) uses the enums\n[`Access`](/apps-script/reference/drive/access) and\n[`Permission`](/apps-script/reference/drive/permission) to determine which users\nhave access to a file or folder. In almost all cases, you access these enums\nfrom the global object. For example, a call to the method\n[`Folder.setSharing(accessType, permissionType)`](/apps-script/reference/drive/folder#setSharing(Access,Permission))\nlooks like this: \n\n // Creates a folder that anyone on the Internet can read from and write to. (Domain administrators can\n // prohibit this setting for Google Workspace users.)\n var folder = DriveApp.createFolder('Shared Folder');\n folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);"]]