5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

スプレッドシートのデータをマスキングするスクリプトを定期実行する

Last updated at Posted at 2022-03-16

やりたいこと

Googleフォームで連携したデータを、個人情報を削いで別シートに貼り付け、元データを消す
以上を定期実行でやりたい

準備

Googleフォーム(この記事では割愛します)
Googleスプレッドシート

実装

データとシートを準備

Googleフォームからは「氏名、フリガナ、性別、電話番号、生年月日、症状」のデータが連携されるとします
スクリーンショット 2022-03-17 1.09.42.png

氏名とフリガナを削いで別シートに保存するのが目的です
スクリーンショット 2022-03-17 1.09.53.png

マクロを記録して、大体のスクリプトを用意

すこし横着をして、実際に作業を行ってみて大体のスクリプトを作ってもらいます
スクリーンショット 2022-03-17 1.10.08.png
macro.gif

台本を書きます

こういう処理をしてほしいんだよなぁ〜というのを書いて、記録されたマクロと見比べてみましょう
スクリーンショット 2022-03-17 1.26.29.png

台本に合わせて記録したスクリプトを分解

案外そのまま使えるのがないですね。やりたいことを地道にひとつひとつ検索して潰していきましょう
スクリーンショット 2022-03-17 1.32.31.png

実装

書いてはデバッグで試しを繰り返して動作するようになりました

function test() {
  // シート1の性別にカーソルを合わせる
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('シート1'), true);
  // シート1の性別のすぐ下にデータがあるか確認
  var cell = spreadsheet.getRange("C2");
  if (cell.isBlank()) {
    // データがない場合(1) ===============
    // 終了
  } else {
    // データがある場合(1) ===============
    // コピーする範囲を求める
    var lastRow = spreadsheet.getLastRow();
    var copyRange = '\'シート1\'!C2:F' + lastRow
    // シート「マスキングデータ」を開く
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('マスキングデータ'), true);
    // シート「マスキングデータ」の性別のすぐ下にデータがあるか確認
    var cell = spreadsheet.getRange("A2");
    if (cell.isBlank()) {
      // データがない場合(2) ---------------
      // データをコピー
      spreadsheet.getRange(copyRange).copyTo(cell, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false)
      // シート1のデータを削除
      clear_sheet1()
    } else {
      // データがある場合(2) ---------------
      // 最後のデータを選択
      var lastRow = spreadsheet.getLastRow();
      // その下を選択
           lastRow = lastRow + 1
      var cell = spreadsheet.getRange("A" + lastRow);
      // データをコピー
      spreadsheet.getRange(copyRange).copyTo(cell, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false)
      // シート1のデータを削除
      clear_sheet1()
    };
  };
};

// ここはまんまマクロで記録したものが使えました
function clear_sheet1() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('シート1'), true);
  spreadsheet.getRange('A2').activate();
  var currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  currentCell.activateAsCurrentCell();
  currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
  currentCell.activateAsCurrentCell();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
};

定期実行の設定

AppScriptのトリガーを選択し
スクリーンショット 2022-03-17 2.13.24.png

スクリーンショット 2022-03-17 2.13.32.png

イベントのソースを「時間主導型」にし、処理が実行される時間を指定します
スクリーンショット 2022-03-17 2.14.00.png

5
4
1

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?