Hướng dẫn nhanh này tạo một tiện ích bổ sung đơn giản cho Google Workspace, minh hoạ trang chủ, các điều kiện kích hoạt theo bối cảnh và cách kết nối với các API của bên thứ ba.
Tiện ích bổ sung này tạo ra các giao diện theo ngữ cảnh và không theo ngữ cảnh trong Gmail, Lịch và Drive. Tiện ích này hiển thị một hình ảnh ngẫu nhiên về một chú mèo có văn bản phủ lên hình ảnh. Văn bản này là văn bản tĩnh cho trang chủ hoặc được lấy từ ngữ cảnh ứng dụng lưu trữ cho các điều kiện kích hoạt theo bối cảnh.
Mục tiêu
- Thiết lập môi trường.
- Thiết lập tập lệnh.
- Chạy tập lệnh.
Điều kiện tiên quyết
Để sử dụng mẫu này, bạn cần đáp ứng các điều kiện tiên quyết sau:
- Một Tài khoản Google (tài khoản Google Workspace có thể yêu cầu quản trị viên phê duyệt).
Một trình duyệt web có quyền truy cập vào Internet.
Thiết lập môi trường
Mở dự án trên Cloud trong Google Cloud Console
Nếu chưa mở, hãy mở dự án trên Cloud mà bạn dự định dùng cho mẫu này:
- Trong Google Cloud Console, hãy chuyển đến trang Chọn một dự án.
- Chọn dự án trong Google Cloud mà bạn muốn sử dụng. Hoặc nhấp vào Tạo dự án rồi làm theo hướng dẫn trên màn hình. Nếu tạo một dự án trên Google Cloud, bạn có thể cần bật tính năng thanh toán cho dự án đó.
Định cấu hình màn hình xin phép bằng OAuth
Tiện ích bổ sung của Google Workspace yêu cầu phải có cấu hình màn hình đồng ý. Việc định cấu hình màn hình đồng ý OAuth của tiện ích bổ sung sẽ xác định nội dung mà Google hiển thị cho người dùng.
- Trong bảng điều khiển Google Cloud, hãy chuyển đến phần Trình đơn > > Thương hiệu.
- Nếu đã định cấu hình , bạn có thể định cấu hình các chế độ cài đặt sau đây cho Màn hình đồng ý OAuth trong phần Thương hiệu, Đối tượng và Quyền truy cập vào dữ liệu. Nếu bạn thấy thông báo chưa được định cấu hình, hãy nhấp vào Bắt đầu:
- Trong mục Thông tin về ứng dụng, trong phần Tên ứng dụng, hãy nhập tên cho ứng dụng.
- Trong phần Email hỗ trợ người dùng, hãy chọn một địa chỉ email hỗ trợ mà người dùng có thể liên hệ với bạn nếu họ có thắc mắc về sự đồng ý của mình.
- Nhấp vào Tiếp theo.
- Trong phần Đối tượng người xem, hãy chọn Nội bộ.
- Nhấp vào Tiếp theo.
- Trong phần Thông tin liên hệ, hãy nhập Địa chỉ email để bạn có thể nhận thông báo về mọi thay đổi đối với dự án của mình.
- Nhấp vào Tiếp theo.
- Trong phần Hoàn tất, hãy xem kỹ Chính sách dữ liệu người dùng của dịch vụ API của Google. Nếu bạn đồng ý, hãy chọn Tôi đồng ý với Chính sách dữ liệu người dùng của dịch vụ API của Google.
- Nhấp vào Tiếp tục.
- Nhấp vào Tạo.
- Hiện tại, bạn có thể bỏ qua bước thêm phạm vi. Trong tương lai, khi tạo một ứng dụng để sử dụng bên ngoài tổ chức Google Workspace, bạn phải thay đổi Loại người dùng thành Bên ngoài. Sau đó, hãy thêm các phạm vi uỷ quyền mà ứng dụng của bạn yêu cầu. Để tìm hiểu thêm, hãy xem hướng dẫn đầy đủ về cách Định cấu hình sự đồng ý của OAuth.
Thiết lập tập lệnh
Tạo dự án Apps Script
- Để tạo một dự án Apps Script mới, hãy truy cập vào script.new.
- Nhấp vào Dự án chưa có tiêu đề.
- Đổi tên dự án Apps Script thành Cats rồi nhấp vào Rename (Đổi tên).
- Bên cạnh tệp
Code.gs
, hãy nhấp vào biểu tượng Tuỳ chọn khác > Đổi tên. Đặt tênCommon
cho tệp. - Nhấp vào biểu tượng Thêm tệp > Tập lệnh. Đặt tên
Gmail
cho tệp. - Lặp lại bước 5 để tạo thêm 2 tệp kịch bản có tên
Calendar
vàDrive
. Khi hoàn tất, bạn sẽ có 4 tệp kịch bản riêng biệt. Thay thế nội dung của từng tệp bằng đoạn mã tương ứng sau:
Common.gs
/** * This simple Google Workspace add-on shows a random image of a cat in the * sidebar. When opened manually (the homepage card), some static text is * overlayed on the image, but when contextual cards are opened a new cat image * is shown with the text taken from that context (such as a message's subject * line) overlaying the image. There is also a button that updates the card with * a new random cat image. * * Click "File > Make a copy..." to copy the script, and "Publish > Deploy from * manifest > Install add-on" to install it. */ /** * The maximum number of characters that can fit in the cat image. */ var MAX_MESSAGE_LENGTH = 40; /** * Callback for rendering the homepage card. * @return {CardService.Card} The card to show to the user. */ function onHomepage(e) { console.log(e); var hour = Number(Utilities.formatDate(new Date(), e.userTimezone.id, 'H')); var message; if (hour >= 6 && hour < 12) { message = 'Good morning'; } else if (hour >= 12 && hour < 18) { message = 'Good afternoon'; } else { message = 'Good night'; } message += ' ' + e.hostApp; return createCatCard(message, true); } /** * Creates a card with an image of a cat, overlayed with the text. * @param {String} text The text to overlay on the image. * @param {Boolean} isHomepage True if the card created here is a homepage; * false otherwise. Defaults to false. * @return {CardService.Card} The assembled card. */ function createCatCard(text, isHomepage) { // Explicitly set the value of isHomepage as false if null or undefined. if (!isHomepage) { isHomepage = false; } // Use the "Cat as a service" API to get the cat image. Add a "time" URL // parameter to act as a cache buster. var now = new Date(); // Replace forward slashes in the text, as they break the CataaS API. var caption = text.replace(/\//g, ' '); var imageUrl = Utilities.formatString('https://cataas.com/cat/says/%s?time=%s', encodeURIComponent(caption), now.getTime()); var image = CardService.newImage() .setImageUrl(imageUrl) .setAltText('Meow') // Create a button that changes the cat image when pressed. // Note: Action parameter keys and values must be strings. var action = CardService.newAction() .setFunctionName('onChangeCat') .setParameters({text: text, isHomepage: isHomepage.toString()}); var button = CardService.newTextButton() .setText('Change cat') .setOnClickAction(action) .setTextButtonStyle(CardService.TextButtonStyle.FILLED); var buttonSet = CardService.newButtonSet() .addButton(button); // Create a footer to be shown at the bottom. var footer = CardService.newFixedFooter() .setPrimaryButton(CardService.newTextButton() .setText('Powered by cataas.com') .setOpenLink(CardService.newOpenLink() .setUrl('https://cataas.com'))); // Assemble the widgets and return the card. var section = CardService.newCardSection() .addWidget(image) .addWidget(buttonSet); var card = CardService.newCardBuilder() .addSection(section) .setFixedFooter(footer); if (!isHomepage) { // Create the header shown when the card is minimized, // but only when this card is a contextual card. Peek headers // are never used by non-contexual cards like homepages. var peekHeader = CardService.newCardHeader() .setTitle('Contextual Cat') .setImageUrl('https://www.gstatic.com/images/icons/material/system/1x/pets_black_48dp.png') .setSubtitle(text); card.setPeekCardHeader(peekHeader) } return card.build(); } /** * Callback for the "Change cat" button. * @param {Object} e The event object, documented {@link * https://developers.google.com/gmail/add-ons/concepts/actions#action_event_objects * here}. * @return {CardService.ActionResponse} The action response to apply. */ function onChangeCat(e) { console.log(e); // Get the text that was shown in the current cat image. This was passed as a // parameter on the Action set for the button. var text = e.parameters.text; // The isHomepage parameter is passed as a string, so convert to a Boolean. var isHomepage = e.parameters.isHomepage === 'true'; // Create a new card with the same text. var card = createCatCard(text, isHomepage); // Create an action response that instructs the add-on to replace // the current card with the new one. var navigation = CardService.newNavigation() .updateCard(card); var actionResponse = CardService.newActionResponseBuilder() .setNavigation(navigation); return actionResponse.build(); } /** * Truncate a message to fit in the cat image. * @param {string} message The message to truncate. * @return {string} The truncated message. */ function truncate(message) { if (message.length > MAX_MESSAGE_LENGTH) { message = message.slice(0, MAX_MESSAGE_LENGTH); message = message.slice(0, message.lastIndexOf(' ')) + '...'; } return message; }
Gmail.gs
/** * Callback for rendering the card for a specific Gmail message. * @param {Object} e The event object. * @return {CardService.Card} The card to show to the user. */ function onGmailMessage(e) { console.log(e); // Get the ID of the message the user has open. var messageId = e.gmail.messageId; // Get an access token scoped to the current message and use it for GmailApp // calls. var accessToken = e.gmail.accessToken; GmailApp.setCurrentMessageAccessToken(accessToken); // Get the subject of the email. var message = GmailApp.getMessageById(messageId); var subject = message.getThread().getFirstMessageSubject(); // Remove labels and prefixes. subject = subject .replace(/^([rR][eE]|[fF][wW][dD])\:\s*/, '') .replace(/^\[.*?\]\s*/, ''); // If neccessary, truncate the subject to fit in the image. subject = truncate(subject); return createCatCard(subject); } /** * Callback for rendering the card for the compose action dialog. * @param {Object} e The event object. * @return {CardService.Card} The card to show to the user. */ function onGmailCompose(e) { console.log(e); var header = CardService.newCardHeader() .setTitle('Insert cat') .setSubtitle('Add a custom cat image to your email message.'); // Create text input for entering the cat's message. var input = CardService.newTextInput() .setFieldName('text') .setTitle('Caption') .setHint('What do you want the cat to say?'); // Create a button that inserts the cat image when pressed. var action = CardService.newAction() .setFunctionName('onGmailInsertCat'); var button = CardService.newTextButton() .setText('Insert cat') .setOnClickAction(action) .setTextButtonStyle(CardService.TextButtonStyle.FILLED); var buttonSet = CardService.newButtonSet() .addButton(button); // Assemble the widgets and return the card. var section = CardService.newCardSection() .addWidget(input) .addWidget(buttonSet); var card = CardService.newCardBuilder() .setHeader(header) .addSection(section); return card.build(); } /** * Callback for inserting a cat into the Gmail draft. * @param {Object} e The event object. * @return {CardService.UpdateDraftActionResponse} The draft update response. */ function onGmailInsertCat(e) { console.log(e); // Get the text that was entered by the user. var text = e.formInput.text; // Use the "Cat as a service" API to get the cat image. Add a "time" URL // parameter to act as a cache buster. var now = new Date(); var imageUrl = 'https://cataas.com/cat'; if (text) { // Replace forward slashes in the text, as they break the CataaS API. var caption = text.replace(/\//g, ' '); imageUrl += Utilities.formatString('/says/%s?time=%s', encodeURIComponent(caption), now.getTime()); } var imageHtmlContent = '<img style="display: block; max-height: 300px;" src="' + imageUrl + '"/>'; var response = CardService.newUpdateDraftActionResponseBuilder() .setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction() .addUpdateContent(imageHtmlContent,CardService.ContentType.MUTABLE_HTML) .setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT)) .build(); return response; }
Calendar.gs
/** * Callback for rendering the card for a specific Calendar event. * @param {Object} e The event object. * @return {CardService.Card} The card to show to the user. */ function onCalendarEventOpen(e) { console.log(e); var calendar = CalendarApp.getCalendarById(e.calendar.calendarId); // The event metadata doesn't include the event's title, so using the // calendar.readonly scope and fetching the event by it's ID. var event = calendar.getEventById(e.calendar.id); if (!event) { // This is a new event still being created. return createCatCard('A new event! Am I invited?'); } var title = event.getTitle(); // If necessary, truncate the title to fit in the image. title = truncate(title); return createCatCard(title); }
Drive.gs
/** * Callback for rendering the card for specific Drive items. * @param {Object} e The event object. * @return {CardService.Card} The card to show to the user. */ function onDriveItemsSelected(e) { console.log(e); var items = e.drive.selectedItems; // Include at most 5 items in the text. items = items.slice(0, 5); var text = items.map(function(item) { var title = item.title; // If neccessary, truncate the title to fit in the image. title = truncate(title); return title; }).join('\n'); return createCatCard(text); }
Nhấp vào Project Settings (Cài đặt dự án)
.
Đánh dấu vào hộp Hiển thị tệp kê khai "appsscript.json" trong trình chỉnh sửa.
Nhấp vào Trình chỉnh sửa
.Mở tệp
appsscript.json
rồi thay thế nội dung bằng đoạn mã sau, sau đó nhấp vào Lưu.
appsscript.json
{ "timeZone": "America/New_York", "dependencies": { }, "exceptionLogging": "STACKDRIVER", "oauthScopes": [ "https://www.googleapis.com/auth/calendar.addons.execute", "https://www.googleapis.com/auth/calendar.readonly", "https://www.googleapis.com/auth/drive.addons.metadata.readonly", "https://www.googleapis.com/auth/gmail.addons.current.action.compose", "https://www.googleapis.com/auth/gmail.addons.current.message.readonly", "https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/script.locale"], "runtimeVersion": "V8", "addOns": { "common": { "name": "Cats", "logoUrl": "https://www.gstatic.com/images/icons/material/system/1x/pets_black_48dp.png", "useLocaleFromApp": true, "homepageTrigger": { "runFunction": "onHomepage", "enabled": true }, "universalActions": [{ "label": "Learn more about Cataas", "openLink": "https://cataas.com" }] }, "gmail": { "contextualTriggers": [{ "unconditional": { }, "onTriggerFunction": "onGmailMessage" }], "composeTrigger": { "selectActions": [{ "text": "Insert cat", "runFunction": "onGmailCompose" }], "draftAccess": "NONE" } }, "drive": { "onItemsSelectedTrigger": { "runFunction": "onDriveItemsSelected" } }, "calendar": { "eventOpenTrigger": { "runFunction": "onCalendarEventOpen" } } } }
Sao chép số dự án trên Cloud
- Trong Bảng điều khiển Google Cloud, hãy chuyển đến phần Trình đơn > IAM và Quản trị > Cài đặt.
- Sao chép giá trị trong trường Số dự án.
Đặt dự án Cloud cho dự án Apps Script
- Trong dự án Apps Script, hãy nhấp vào Cài đặt dự án
.
- Trong mục Dự án trên Google Cloud Platform (GCP), hãy nhấp vào Thay đổi dự án.
- Trong Số dự án trên Google Cloud Platform, hãy dán số dự án trên Google Cloud.
- Nhấp vào Đặt dự án.
Cài đặt bản triển khai thử nghiệm
- Trong dự án Apps Script, hãy nhấp vào Trình chỉnh sửa .
- Nhấp vào Triển khai > Kiểm thử việc triển khai.
- Nhấp vào Cài đặt > Xong.
Chạy tập lệnh
- Truy cập vào Gmail.
- Để mở tiện ích bổ sung này, trong bảng điều khiển bên phải, hãy nhấp vào biểu tượng Mèo .
- Nếu được nhắc, hãy cho phép tiện ích bổ sung.
- Tiện ích bổ sung này hiển thị một hình ảnh và văn bản về mèo. Để thay đổi hình ảnh, hãy nhấp vào Thay đổi mèo.
- Nếu bạn mở một email trong khi tiện ích đang mở, hình ảnh sẽ làm mới và văn bản sẽ thay đổi thành dòng tiêu đề của email (bị cắt bớt nếu quá dài).
Bạn có thể thấy chức năng tương tự trong Lịch và Drive. Bạn không cần cấp quyền lại cho tiện ích bổ sung để sử dụng tiện ích này trong các ứng dụng lưu trữ đó.
Các bước tiếp theo
- Mở rộng Google Workspace bằng tiện ích bổ sung
- Tạo tiện ích bổ sung cho Google Workspace
- Phát hành ứng dụng