0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スプレッドシートの標準で行えない一括設定をchatGPTのプロンプトで解決

0
Posted at

やりたいこと

あるスプレッドシートの保護されているシートと範囲の権限をカスタムで全員チェックをつける設定に一括で行いたい。

プロンプト例

以下が投げたプロンプト例です

Googleスプレッドシート(現在開いているファイル)内の「保護されているシート」と「保護されている範囲」をすべて対象に、権限を一括変更したいです。 目的は 「権限:カスタム」→ このスプレッドシートの編集者全員が編集できる状態 に揃えることです。 既存の編集可能ユーザーは 置き換え(リセットして編集者全員に統一) してください。 Apps Scriptで、実行手順とコードのみを提示してください。

chatGPTからのレスポンス

実行手順(Apps Script)

  • 対象のスプレッドシートを開く
  • メニュー 拡張機能 → Apps Script を開く
  • エディタに下のコードを貼り付け(既存コードは上書きでOK)
  • 保存(💾)
  • 上部の関数選択で▶実行
  • 初回のみ、権限承認(画面の指示に従う)
  • 実行ログは 表示 → ログ で確認

コード(Google Apps Script)

/**
 * 既存の「保護されているシートと範囲」を全部拾って、
 * 権限(編集できる人)を一括で揃える。
 *
 * MODE:
 *  - "SPREADSHEET_EDITORS": そのファイルの編集者全員を編集可にする(おすすめ)
 *  - "DOMAIN_EVERYONE":     (可能なら)ドメイン全員を編集可にする
 *  - "BOTH":                両方(ドメイン + 編集者)
 */
function applyToAllProtections() {
  const MODE = "SPREADSHEET_EDITORS"; // ←ここだけ目的に合わせて変更

  const ss = SpreadsheetApp.getActive();
  const myEmail = Session.getEffectiveUser().getEmail();

  // 「このファイルの編集者」一覧(※共有がグループ経由だと完全に拾えない場合あり)
  const editorEmails = ss.getEditors().map(u => u.getEmail()).filter(Boolean);

  const protections = []
    .concat(ss.getProtections(SpreadsheetApp.ProtectionType.SHEET))
    .concat(ss.getProtections(SpreadsheetApp.ProtectionType.RANGE));

  let ok = 0, skipped = 0, failed = 0;

  protections.forEach((p) => {
    try {
      // 編集権限がない保護は触れない
      if (!p.canEdit()) { skipped++; return; }

      // 「警告のみ」ではなく、権限設定が効くモードに寄せたい場合
      p.setWarningOnly(false);

      // removeEditors でグループ由来の権限を外すと例外になることがあるので、
      // 公式サンプルにならって一旦自分を editor に入れてから触る :contentReference[oaicite:1]{index=1}
      p.addEditor(myEmail);

      if (MODE === "DOMAIN_EVERYONE" || MODE === "BOTH") {
        if (p.canDomainEdit()) {
          p.setDomainEdit(true);
        }
      } else {
        // ドメイン全員は許可しない(揃えたい場合)
        if (p.canDomainEdit()) {
          p.setDomainEdit(false);
        }
      }

      if (MODE === "SPREADSHEET_EDITORS" || MODE === "BOTH") {
        // 既存 editors を一旦外してから、編集者全員を追加(UIで全員にチェックするイメージ)
        const currentEditors = p.getEditors().map(u => u.getEmail()).filter(Boolean);
        if (currentEditors.length) p.removeEditors(currentEditors);
        if (editorEmails.length) p.addEditors(editorEmails);
      }

      ok++;
    } catch (e) {
      failed++;
      console.warn("Failed on protection:", p.getDescription(), e);
    }
  });

  console.log({ total: protections.length, ok, skipped, failed });
}

感想

無事一括で権限を与えることができました。
手作業だと数時間かかりそうな量が、5分ほどで対応できたのでスッキリ。
セキュリティやインジェクションに注意して、どんどんAIに頼るべきですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?