Quick tip: Review the prerequisites before you run the lab
Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
/**
* Copyright Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 現在のメールスレッド用にレンダリングするカードの
* 配列を返す。この関数の名前は
* マニフェストの 'onTriggerFunction' フィールドに指定される。これは、この関数が
* アドオン開始のたびに実行されることを意味する。
*
* @param {Object} e Gmail の UI によって提供されるデータ。
* @return {Card[]}
*/
function buildAddOn(e) {
// 一時的な Gmail アドオン スコープを有効にする。*.
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var messageId = e.messageMetadata.messageId;
var message = GmailApp.getMessageById(messageId);
// ユーザーとスレッドのラベルは、素早く並べ替えてインデックス登録ができるように配列として取得する。
var threadLabels = message.getThread().getLabels();
var labels = getLabelArray(GmailApp.getUserLabels());
var labelsInUse = getLabelArray(threadLabels);
// ユーザーラベルすべてを含むセクションを作成する。
var section = CardService.newCardSection()
.setHeader("<font color=\"#1257e0\"><b>Available User Labels</b></font>");
// 前のセクションに追加されたユーザーラベルのチェックボックス グループを作成する。
var checkboxGroup = CardService.newSelectionInput()
.setType(CardService.SelectionInputType.CHECK_BOX)
.setFieldName('labels')
.setOnChangeAction(CardService.newAction().setFunctionName('toggleLabel'));
// Add checkbox with 各ユーザーラベルの名前と選択した値を含むチェックボックスを追加する。
for(var i = 0; i < labels.length; i++) {
checkboxGroup.addItem(labels[i], labels[i], labelsInUse.indexOf(labels[i])!= -1);
}
// チェックボックス グループをセクションに追加する。
section.addWidget(checkboxGroup);
// セクションを追加した後にメインカードを作成する。
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle('Quick Label')
.setImageUrl('https://www.gstatic.com/images/icons/material/system/1x/label_googblue_48dp.png'))
.addSection(section)
.build();
return [card];
}
/**
* ユーザーの選択に基づき、現在のスレッドのラベルを
* 更新する。作成した CHECK_BOX ごとに
* OnChangeAction を介して実行する。
*
* @param {Object} e Gmail の UI によって提供されるデータ。
*/
function toggleLabel(e){
var selected = e.formInputs.labels;
// 一時的な Gmail アドオン スコープを有効にする。
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var messageId = e.messageMetadata.messageId;
var message = GmailApp.getMessageById(messageId);
var thread = message.getThread();
if (selected != null){
for each (var label in GmailApp.getUserLabels()) {
if(selected.indexOf(label.getName()) != -1){
thread.addLabel(label);
}
else {
thread.removeLabel(label);
}
}
}
else {
for each (var label in GmailApp.getUserLabels()) {
thread.removeLabel(label);
}
}
}
/**
* GmailLabel オブジェクトを文字列の配列に変換する。
* 簡単な並べ替え、または値が存在するかどうかの判断に使用される。
*
* @param {labelsObjects} GmailLabel オブジェクトの配列。
* @return {lables[]} ラベル名の文字列としての配列。
*/
function getLabelArray(labelsObjects){
var labels = [];
for(var i = 0; i < labelsObjects.length; i++) {
labels[i] = labelsObjects[i].getName();
}
labels.sort();
return labels;
}
注: 保存する前に、エラーを回避するためにスクリプト マニフェストを更新する必要があります。
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
Use private browsing
Copy the provided Username and Password for the lab
Click Open console in private mode
Sign in to the Console
Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
Accept the terms, and skip the recovery resource page
Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project
このコンテンツは現在ご利用いただけません
利用可能になりましたら、メールでお知らせいたします
ありがとうございます。
利用可能になりましたら、メールでご連絡いたします
One lab at a time
Confirm to end all existing labs and start this one
Use private browsing to run the lab
Use an Incognito or private browser window to run this lab. This
prevents any conflicts between your personal account and the Student
account, which may cause extra charges incurred to your personal account.