Admin SDK グループ移行サービス

メールを Google グループのディスカッション アーカイブに移行するスクリプト。

Admin SDK Groups Migration サービスを使用すると、Google Apps Script で Admin SDK の Groups Migration API を使用できます。この API を使用すると、Google Workspace ドメインの管理者(販売パートナー様を含む)は、パブリック フォルダやメーリング リストから Google グループのディスカッション アーカイブにメールを移行できます。

これは、使用前に有効にする必要がある拡張サービスです。

リファレンス

このサービスの詳細については、Admin SDK Groups Migration API のリファレンス ドキュメントをご覧ください。Apps Script のすべての高度なサービスと同様に、Admin SDK Groups Migration サービスでは、公開 API と同じオブジェクト、メソッド、パラメータを使用します。詳細については、メソッドのシグネチャの決定方法をご覧ください。

問題を報告したり、その他のサポートを利用したりするには、Admin SDK Groups Migration に関するサポートガイドをご覧ください。

サンプルコード

次のサンプルコードでは、API のバージョン 1 を使用します。

Gmail から Google グループにメールを移行する

このサンプルでは、ユーザーの Gmail 受信トレイの最新の 3 つのスレッドから RFC 822 形式のメッセージを 3 つ取得し、メール コンテンツ(添付ファイルを含む)から Blob を作成して、ドメイン内の Google グループに挿入します。

advanced/adminSDK.gs
/**
 * Gets three RFC822 formatted messages from the each of the latest three
 * threads in the user's Gmail inbox, creates a blob from the email content
 * (including attachments), and inserts it in a Google Group in the domain.
 */
function migrateMessages() {
  // TODO (developer) - Replace groupId value with yours
  const groupId = "exampleGroup@example.com";
  const messagesToMigrate = getRecentMessagesContent();
  for (const messageContent of messagesToMigrate) {
    const contentBlob = Utilities.newBlob(messageContent, "message/rfc822");
    AdminGroupsMigration.Archive.insert(groupId, contentBlob);
  }
}

/**
 * Gets a list of recent messages' content from the user's Gmail account.
 * By default, fetches 3 messages from the latest 3 threads.
 *
 * @return {Array} the messages' content.
 */
function getRecentMessagesContent() {
  const NUM_THREADS = 3;
  const NUM_MESSAGES = 3;
  const threads = GmailApp.getInboxThreads(0, NUM_THREADS);
  const messages = GmailApp.getMessagesForThreads(threads);
  const messagesContent = [];
  for (let i = 0; i < messages.length; i++) {
    for (let j = 0; j < NUM_MESSAGES; j++) {
      const message = messages[i][j];
      if (message) {
        messagesContent.push(message.getRawContent());
      }
    }
  }
  return messagesContent;
}