Apps Script 快速入門導覽課程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文說明如何使用 Apps Script 傳送含有結構化資料的電子郵件給自己,藉此測試電子郵件標記。
建立專案
前往 script.google.com。如果您是第一次前往 script.google.com
,系統會將您重新導向至資訊頁面。按一下「Start Scripting」(開始編寫指令碼),前往指令碼編輯器。在指令碼編輯器中,為「空白專案」建立指令碼。
將 Code.gs
中的程式碼替換為下列程式碼:
依序選取「File」>「New」>「Html file」,即可建立新的 HTML 檔案。將檔案命名為 mail_template
,與上述 JavaScript 中的參數相符。將 HTML 檔案內容替換成下列內容:
測試指令碼
如要測試指令碼,請執行下列操作:
- 儲存專案。
- 選取「
Code.gs
」分頁。
- 確認已在
Select function
下拉式選單中選取 testSchemas
函式。
- 在 Apps Script 開發環境中按一下
Run
。
第一次執行指令碼時,系統會要求您授予授權,之後請重新執行指令碼。指令碼執行完畢後,請檢查收件匣中是否有自己寄出的電子郵件,內含「前往動作」按鈕,如下列螢幕截圖所示:
指令碼的運作方式
testSchemas
函式會從名為 mail_template.html
的檔案讀取 HTML 內容,並以電子郵件的形式將該內容傳送給目前已通過驗證的使用者。如「向 Google 註冊」一文所述,您傳送給自己的所有結構化資料都會顯示在 Gmail 中,因此您可以使用指令碼傳送的電子郵件,在測試時忽略註冊規定。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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."]]