Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Configure the OAuth consent screen
/ 30
Create an Apps Script project
/ 30
Apply sentiment analysis
/ 40
Configure the OAuth consent screen
/ 30
Create an Apps Script project
/ 30
Apply sentiment analysis
/ 40
增益集可擴充 Google Workspace 功能,協助使用者提高工作效率並改善工作流程。在本實驗室中,您將建構 Google Workspace 外掛程式,運用 Gemini 和 Agent Platform 的功能分析 Gmail 內容的情緒。您會設定必要的雲端資源 (包括 Agent Platform API)、設定 Apps Script 專案,並部署外掛程式。
這項外掛程式可自動辨識並標示語氣負面的電子郵件,您可藉此優先處理客服回覆,或快速找出可能含有敏感內容的電子郵件。
完成實驗室活動後,您會有一個可實際運用 AI 的工具,提升業務效率和溝通品質。
在本實驗室,您將執行下列作業:
請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。
您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。
為了順利完成這個實驗室,請先確認:
點選「Start Lab」按鈕。如果實驗室會產生費用,畫面上會出現選擇付款方式的對話方塊。左側的「Lab Details」窗格會顯示下列項目:
點選「Open Google Cloud console」;如果使用 Chrome 瀏覽器,也能按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」。
接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。
提示:您可以在不同的視窗中並排開啟分頁。
如有必要,請將下方的 Username 貼到「登入」對話方塊。
您也可以在「Lab Details」窗格找到 Username。
點選「下一步」。
複製下方的 Password,並貼到「歡迎使用」對話方塊。
您也可以在「Lab Details」窗格找到 Password。
點選「下一步」。
按過後續的所有頁面:
Google Cloud 控制台稍後會在這個分頁開啟。
您已登入 Google Cloud 控制台,接著登入 Gmail。
提示:將分頁安排在不同視窗,方便查看。
現在可以開始做實驗室活動了!
在這個步驟中,您會啟用 Agent Platform API,然後設定 OAuth 同意畫面,選定 Google Workspace 向使用者顯示的內容。
前往 Google Cloud 控制台,依序點選「導覽選單」>「API 和服務」>「程式庫」。
在「搜尋 API 和服務」方塊中輸入「Agent Platform API」,然後在搜尋結果中點選「Agent Platform API」。
點選「啟用」來啟用 API。
「API/服務詳細資料」頁面隨即開啟。
在左側窗格中,按一下「OAuth 同意畫面」。
點選「開始使用」。
在「應用程式資訊」部分,設定下列項目,然後點選「下一步」:
點選「Check my progress」,確認目標已達成。
在這項工作中,您會建立外掛程式並設定為 Apps Script 專案。
如要取得 Google Cloud 專案編號,以便在建立 Apps Script 專案時使用,請按照下列步驟操作:
前往導覽選單 (),依序點選「Cloud 總覽」>「資訊主頁」。
在「專案資訊」部分,記下專案編號,之後會在本實驗室用到。
在「Student Resources」窗格中,點選連結「script.google.com/」,開啟 Apps Script 頁面。
點選「新專案」,建立 Apps Script 專案。
為專案命名:
顯示資訊清單檔案:
變更 Google Cloud Platform 專案:
點選「Check my progress」,確認目標已達成。
請按照下列操作說明,使用程式碼範例更新專案。
appsscript.json,將檔案內容替換為下列內容:Hello, You are late in delivery, again.
Please contact me ASAP before I cancel our subscription.
`, name: 'Customer C' } ]; // Send each sample email for (const email of sampleEmails) { GmailApp.sendEmail(userEmail, email.subject, email.body, { name: email.name, htmlBody: email.htmlBody }); } // Return a notification return buildNotificationResponse("Successfully generated sample emails"); } /** * Analyzes the sentiment of the first 10 threads in the inbox * and labels them accordingly. * * @returns {ActionResponse} - A notification confirming completion. */ function analyzeSentiment() { // Analyze and label emails analyzeAndLabelEmailSentiment(); // Return a notification return buildNotificationResponse("Successfully completed sentiment analysis"); } /** * Analyzes the sentiment of emails and applies appropriate labels. */ function analyzeAndLabelEmailSentiment() { // Define label names const labelNames = ["HAPPY TONE 😊", "NEUTRAL TONE 😐", "UPSET TONE 😡"]; // Get or create labels for each sentiment const positiveLabel = GmailApp.getUserLabelByName(labelNames[0]) || GmailApp.createLabel(labelNames[0]); const neutralLabel = GmailApp.getUserLabelByName(labelNames[1]) || GmailApp.createLabel(labelNames[1]); const negativeLabel = GmailApp.getUserLabelByName(labelNames[2]) || GmailApp.createLabel(labelNames[2]); // Get the first 10 threads in the inbox const threads = GmailApp.getInboxThreads(0, 10); // Iterate through each thread for (const thread of threads) { // Iterate through each message in the thread const messages = thread.getMessages(); for (const message of messages) { // Get the plain text body of the message const emailBody = message.getPlainBody(); // Analyze the sentiment of the email body const sentiment = processSentiment(emailBody); // Apply the appropriate label based on the sentiment if (sentiment === 'positive') { thread.addLabel(positiveLabel); } else if (sentiment === 'neutral') { thread.addLabel(neutralLabel); } else if (sentiment === 'negative') { thread.addLabel(negativeLabel); } } } } /** * Sends the email text to Agent Platform for sentiment analysis. * * @param {string} emailText - The text of the email to analyze. * @returns {string} - The sentiment of the email ('positive', 'negative', or 'neutral'). */ function processSentiment(emailText) { // Construct the API endpoint URL const apiUrl = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent`; // Prepare the request payload const payload = { contents: [ { role: "user", parts: [ { text: `Analyze the sentiment of the following message: ${emailText}` } ] } ], generationConfig: { temperature: 0.9, maxOutputTokens: 1024, responseMimeType: "application/json", // Expected response format for simpler parsing. responseSchema: { type: "object", properties: { response: { type: "string", enum: ["positive", "negative", "neutral"] } } } } }; // Prepare the request options const options = { method: 'POST', headers: { 'Authorization': `Bearer ${ScriptApp.getOAuthToken()}` }, contentType: 'application/json', muteHttpExceptions: true, // Set to true to inspect the error response payload: JSON.stringify(payload) }; // Make the API request const response = UrlFetchApp.fetch(apiUrl, options); // Parse the response. There are two levels of JSON responses to parse. const parsedResponse = JSON.parse(response.getContentText()); const sentimentResponse = JSON.parse(parsedResponse.candidates[0].content.parts[0].text).response; // Return the sentiment return sentimentResponse; }
點按「儲存」 () 即可儲存專案。
在這項工作中,您將部署外掛程式,然後驗證安裝作業。
在標題列中,依序點選「部署」>「測試部署作業」。
確認「Gmail」列於「應用程式」下方,然後點選「安裝」。
按一下「完成」。
如果清單中沒有顯示外掛程式,請重新整理瀏覽器視窗。
如果仍未顯示,請返回 Apps Script 專案,從「測試部署」視窗解除安裝外掛程式,然後重新安裝。
現在準備測試外掛程式。您將開啟外掛程式並授權,然後生成電子郵件,驗證分析功能是否正常運作。
在 Gmail 的右側窗格,點選「情緒分析」。
側邊面板開啟後,點選「授予權限」。
同意畫面開啟。選取電子郵件地址 (
同意後,畫面右側會開啟「情緒分析」窗格。
外掛程式現在會產生範例電子郵件,以便測試分析功能。作業完成後,系統會顯示訊息,整個過程只需幾秒鐘。
等待範例電子郵件出現在收件匣中。可能需要重新整理收件匣,才能看到新郵件。
將範例電子郵件傳送至收件匣後,在「情緒分析」窗格中點選「Analyze emails」。
外掛程式畫面底部會顯示分析完成訊息。
這個外掛程式會分析電子郵件,並對收件匣中的郵件套用適當的標籤 (「HAPPY TONE 😊」、「UPSET TONE 😡」或「NEUTRAL TONE 😐」)。
您可能需要重新整理 Gmail,才能看到套用的標籤。
點選「Check my progress」,確認目標已達成。
您已順利完成「使用 Gemini 和 Agent Platform 分析 Gmail 情緒」實驗室。
在本實驗室中,您已學會如何執行下列工作:
現在您已有可正常運作的 Gmail 外掛程式,能協助您排定電子郵件的優先順序,提升工作效率。歡迎進一步測試外掛程式的功能,例如自訂情緒分析或新增功能。
協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。
使用手冊上次更新日期:2025 年 11 月 28 日
實驗室上次測試日期:2025 年 11 月 28 日
Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one
Complete this quick step to start your lab.