Apps 脚本快速入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文介绍了如何使用 Apps 脚本向自己发送包含架构的电子邮件,以便测试电子邮件标记。
创建项目
访问 script.google.com。如果您是首次访问 script.google.com
,系统会将您重定向到信息页面。点击开始编写脚本以继续前往脚本编辑器。在脚本编辑器中,为空白项目创建脚本。
将 Code.gs
中的代码替换为以下代码:
选择文件 > 新建 > HTML 文件以创建新的 HTML 文件。将文件命名为 mail_template
,以与上述 JavaScript 中的参数匹配。将 HTML 文件的内容替换为以下内容:
测试脚本
如需测试脚本,请执行以下操作:
- 保存项目。
- 选择
Code.gs
对应的标签页。
- 确保在
Select function
下拉菜单中选择了函数 testSchemas
。
- 在 Apps 脚本开发环境中,点击
Run
。
首次运行脚本时,系统会要求您授予授权,之后您应重新运行该脚本。脚本运行后,请检查收件箱中是否收到一封自己发给自己的电子邮件,其中包含一个跳转操作按钮,如以下屏幕截图所示:
脚本如何工作?
testSchemas
函数从名为 mail_template.html
的文件中读取 HTML 内容,并将该内容作为电子邮件发送给当前经过身份验证的用户。如向 Google 注册中所述,您发送给自己的所有架构都会显示在 Gmail 中,因此脚本发送的电子邮件可用于忽略注册要求,以进行测试。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Apps Script Quickstart\n\nThis article shows you how to use [Apps Script](/apps-script) to send yourself an email with schemas in order to test email markup.\n\nCreating the project\n--------------------\n\nVisit [script.google.com](https://script.google.com). If this is the first time you've been to `script.google.com`, you'll be redirected to an information page. Click **Start Scripting** to proceed to the script editor. In the script editor, create a script for a **Blank Project**.\n\nReplace the code in `Code.gs` with the following: \ngmail/markup/Code.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/gmail/markup/Code.gs) \n\n```javascript\n/**\n * Send an email with schemas in order to test email markup.\n */\nfunction testSchemas() {\n try {\n const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();\n\n MailApp.sendEmail({\n to: Session.getActiveUser().getEmail(),\n subject: 'Test Email markup - ' + new Date(),\n htmlBody: htmlBody\n });\n } catch (err) {\n console.log(err.message);\n }\n}\n```\n\nSelect **File \\\u003e New \\\u003e Html file** to create a new HTML file. Name the file `mail_template` to match the parameter in the JavaScript above. Replace the content of the HTML file with the following: \ngmail/markup/mail_template.html \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/gmail/markup/mail_template.html) \n\n```html\n\u003c!--\n Copyright 2022 Google LLC\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n--\u003e\n\n\u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"application/ld+json\"\u003e\n {\n \"@context\": \"https://schema.org\",\n \"@type\": \"EmailMessage\",\n \"description\": \"Check this out\",\n \"potentialAction\": {\n \"@type\": \"ViewAction\",\n \"target\": \"https://www.youtube.com/watch?v=eH8KwfdkSqU\"\n }\n }\n \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cp\u003e\n This a test for a Go-To action in Gmail.\n \u003c/p\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nTesting the script\n------------------\n\nTo test the script:\n\n1. Save the project.\n2. Select the tab for `Code.gs`.\n3. Make sure the function `testSchemas` is selected in the `Select function` dropdown menu.\n4. Click `Run` in the Apps Script development environment.\n\nThe first time you run the script you'll be asked to grant authorization, after which you should re-run it. After the script runs, check your inbox for an email sent from yourself with a [Go-To Action](/workspace/gmail/markup/reference/one-click-action) button, as in the following screenshot:\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nHow does the script work?\n-------------------------\n\nThe `testSchemas` function reads the HTML content from the file named `mail_template.html` and sends that content as an email to the currently authenticated user. As explained in [Registering with Google](/workspace/gmail/markup/registering-with-google), all schemas you send to yourself will be displayed in Gmail, so the email sent by the script can be used to ignore the registration requirements for testing purposes."]]