0
1

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 3 years have passed since last update.

GAS 基本編メモ②

Posted at

GASで勉強始めました。
自分用メモです。

処理時間の計算

function initSheet() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var names = ['taguchi', 'fkoji', 'dotinstall'];
  var i;
# ここで変数を定義
  var startTime = new Date();

  sheet.clear();

//  2060ms

  for (i = 1; i <= 1000; i++) {
    sheet.getRange(i, 1).setValue(names[Math.floor(Math.random() * names.length)]);
    sheet.getRange(i, 2).setValue(Math.floor(Math.random() * 101));
  }

# Loggerで処理時間を表示させます
  Logger.log(new Date() - startTime);
}

処理を高速化させる

function initSheet() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var names = ['taguchi', 'fkoji', 'dotinstall'];
  var i;
  var startTime = new Date();
  var scores = [];

  sheet.clear();

//  2060ms

  // for (i = 1; i <= 1000; i++) {
  //   sheet.getRange(i, 1).setValue(names[Math.floor(Math.random() * names.length)]);
  //   sheet.getRange(i, 2).setValue(Math.floor(Math.random() * 101));
  // }

  for (i = 1; i <= 1000; i++) {
    scores.push([
      names[Math.floor(Math.random() * names.length )],
      Math.floor(Math.random() * 101 )
    ]);
  }

  sheet.getRange(1, 1, 1000, 2).setValues(scores);

  Logger.log(new Date() - startTime);
}

判定処理

function initSheet() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var names = ['taguchi', 'fkoji', 'dotinstall'];
  var i;
  var startTime = new Date();
  var scores = [];

  sheet.clear();

  for (i = 1; i <= 10; i++) {
    scores.push([
      names[Math.floor(Math.random() * names.length )],
      Math.floor(Math.random() * 101 )
    ]);
  }

  sheet.getRange(1, 1, 10, 2).setValues(scores);
}

function showResults() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var scores = [];
  var i;
  var results = [];

  // scores = sheet.getRange(1, 2, 10, 1).getValues();
  scores = sheet.getRange(1, 2, sheet.getLastRow(), 1).getValues()

  for (i = 0; i < scores.length; i++) {
    results.push([scores[i] >= 80 ? 'PASS' : 'FAIL']);
  }
  sheet.getRange(1, 3, results.length, 1).setvalues(results);
}
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?