409 Conflictの原因と対処法

406 Not Acceptableの原因と対処法

[GAS][Ui]チェックボックスを実装するには

チェックボックス

▼サンプルスクリプト

黒丸の文字を記述したラベルを「ライト」のように見せている。文字色が黒色の消灯状態で表示し、チェックボックスのON/OFFによってライトを点灯させている。

少し長いコードになるが、このサンプルスクリプトでは、チェックボックスへのクリックイベント追加方法、およびクリックイベントとバインドしたメソッドでチェック状態を取得する方法に注目していただきたい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function doGet() {
  var app     = UiApp.createApplication();
  var wrapper = app.createVerticalPanel();         
  var handler = app.createServerHandler('checkChanged_').addCallbackElement(wrapper);
 
  //チェックボックスを作成
  var checkboxContainer = app.createHorizontalPanel()
    .add(app.createCheckBox('赤色').setName('redChecked').addClickHandler(handler))
    .add(app.createCheckBox('青色').setName('blueChecked').addClickHandler(handler))
    .add(app.createCheckBox('黄色').setName('yellowChecked').addClickHandler(handler));
 
  //ライトを生成
  var lightContainer = app.createHorizontalPanel()
    .add(app.createLabel('●').setId('redLight').setStyleAttribute('fontSize','72px'))
    .add(app.createLabel('●').setId('blueLight').setStyleAttribute('fontSize','72px'))
    .add(app.createLabel('●').setId('yellowLight').setStyleAttribute('fontSize','72px'));
 
  //チェックボックス、ラベルをレイアウトパネルに追加
  wrapper
    .add(app.createHTML('<p>点灯させる色を選択して下さい。(複数選択可)</p>'))
    .add(checkboxContainer)
    .add(lightContainer);
 
  app.add(wrapper);
  return app;
}
 
function checkChanged_(e){
  var app = UiApp.getActiveApplication();
 
  if(e.parameter.redChecked=='true')
    //『赤色』チェックON
    app.getElementById('redLight').setStyleAttribute('color','red');
  else
    //『赤色』チェックOFF
    app.getElementById('redLight').setStyleAttribute('color','black');
 
  if(e.parameter.blueChecked=='true')
    app.getElementById('blueLight').setStyleAttribute('color','blue');
  else
    app.getElementById('blueLight').setStyleAttribute('color','black');
 
  if(e.parameter.yellowChecked=='true')
    app.getElementById('yellowLight').setStyleAttribute('color','yellow');
  else
    app.getElementById('yellowLight').setStyleAttribute('color','black');  
 
  return app;
}

この投稿の続きを読む »

505 HTTP version not supportedの原因と対処法

505 HTTP version not supported

エラー 501/505 – 未実装または未サポート

この投稿の続きを読む »

[GAS][Ui]CSSで装飾するには

装飾したラベル

▼サンプルスクリプト

ラベルやボタン、レイアウトパネルなど、Ui Serviceで生成したUiをsetStyleAttributeで装飾することができる。ここでは、5つのラベルに様々な装飾を施している。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function doGet(e) {
  var app     = UiApp.createApplication();
  var wrapper = app.createVerticalPanel();
 
  //Style属性を設定したラベルを生成
  var label1  = app.createLabel('装飾なし');
  var label2  = app.createLabel('文字サイズ変更')
                   .setStyleAttribute('fontSize', '200%');
  var label3  = app.createLabel('文字の色を変更')
                   .setStyleAttribute('color', 'red');
  var label4  = app.createLabel('枠線')
                   .setStyleAttribute('border', '2px solid #ccc');
  var label5  = app.createLabel('見出し')
                   .setStyleAttribute('fontWeight', 'bold')
                   .setStyleAttribute('fontSize', '200%')
                   .setStyleAttribute('borderLeft','5px solid #ccc')
                   .setStyleAttribute('borderBottom', '1px solid #ccc')
                   .setStyleAttribute('marginTop', '10px');  
  //ラベルをレイアウトパネルに配置
  wrapper
     .add(label1)
     .add(label2)
     .add(label3)
     .add(label4)
     .add(label5);
 
  //レイアウトパネルをUiインスタンスに追加
  app.add(wrapper);  
 
  return app;
}

この投稿の続きを読む »

[GAS][スプレッドシート]処理速度を向上するには

重複行を抽出するスクリプト

このページでは、しばしば速度に難があるといわれるGoogle Apps Scriptの処理速度を向上するための方法を紹介している。今回の例では、約4分かかっていた処理をわずか0.1秒に短縮している。

はじめに結論をいうと、高速化への一番の近道は、APIリクエスト数を可能な限り減らすことだ。

以下に掲載する最適化前、最適化後のスクリプトはともに、都道府県リストから重複行を抽出し、ダイアログメッセージを表示するという目的で作られたもので、実行結果はともに同じものになる。ところが、最適化前は重複データを抽出するのに236,328ミリ秒(約4分)かかっていたのに対し、最適化後は138ミリ秒(0.1秒)で処理が終了している。

▼最適化前のスクリプト

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 最適化前
function notOptimized() {
  var start         = new Date();
  var ss            = SpreadsheetApp.getActiveSpreadsheet();
  var sheet         = ss.getActiveSheet();
  var duplicatedIds = '';
  for(var i=2; i< =sheet.getLastRow(); i++){
    for(var j=2; j<=sheet.getLastRow(); j++){
      if(i==j) continue;
      if(sheet.getRange('B'+i).getValue() == sheet.getRange('B'+j).getValue()){
        duplicatedIds += j + ' ';
      }
    }
  }
  var end  = new Date();
  var span = end - start;
  Logger.log('処理時間:' + span + 'ミリ秒');   
  Browser.msgBox('重複しているのは、行'+duplicatedIds+'です');
}

▼最適化後のスクリプト

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function optimized(){
  var start         = new Date();
  var ss            = SpreadsheetApp.getActiveSpreadsheet();
  var sheet         = ss.getActiveSheet();
  var duplicatedIds = '';
  var values        = sheet.getDataRange().getValues();
  var lastRow       = sheet.getLastRow();
  for(var i=2; i< =lastRow; i++){
    for(var j=2; j<=lastRow; j++){
      if(i==j) continue;
      if(values[i-1][1] == values[j-1][1]){
        duplicatedIds += j + ' ';
      }
    }
  }
  var end  = new Date();
  var span = end - start;
  Logger.log('処理時間:' + span + 'ミリ秒'); 
  Browser.msgBox('重複しているのは、行'+duplicatedIds+'です'); 
}

この投稿の続きを読む »

[GAS][Ui]テキストボックスを実装するには

Google Apps ScriptのテキストボックスUi

▼サンプルスクリプト

税額計算スクリプト。テキストボックスに入力された金額の税込価格を計算して画面に表示する。テキストボックスにKeyUpHandlerを追加し、税額計算メソッドとバインドすることにより、キーが入力されるたびに、金額を再計算して画面に表示する動作を実現している。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function doGet() {
  var app          = UiApp.createApplication();
  var wrapper      = app.createVerticalPanel();
 
  // ラベル、テキストボックスのUIをあらかじめ作成しておく
  var message      = app.createLabel('金額を入力して下さい。税込価格を計算します。');
  var zeinuki      = app.createLabel('税抜き:');
  var textbox      = app.createTextBox().setName('price');
  var yen          = app.createLabel('円');   
  var result       = app.createLabel('').setId('result');
 
  // KeyUpイベントハンドラを作成 
  textbox.addKeyUpHandler(app.createServerHandler('税込価格を計算して表示_')
                          .addCallbackElement(wrapper));
 
  // パネルにコントロールを配置する
  wrapper.add(message);
  wrapper.add(app.createHorizontalPanel()
              .add(zeinuki)
              .add(textbox)
              .add(yen) 
  );
  wrapper.add(result);
 
  app.add(wrapper); 
  return app;
}
 
function 税込価格を計算して表示_(e){
  var app    = UiApp.getActiveApplication();
  var result = app.getElementById('result');
  var price;
 
  // パラメータpriceに1.05を積算
  try{
    price = e.parameter.price * 1.05;
  } catch (e){
    Logger.log(e);   
    return;
  }   
 
  result.setText('税込価格:' + price + '円');   
  return app;
}

この投稿の続きを読む »

501 Not Implementedの原因と対処法

501 Not Implemented

501 Method Not Implemented

この投稿の続きを読む »

[GAS][Ui]Submitボタンを実装するには

Submitボタン

▼サンプルスクリプト

フォームに入力した値をスプレッドシートに挿入するサンプルスクリプト。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// To-Do入力フォームを表示
function doGet() {
  var app = UiApp.createApplication(); 
  var formPanel = app.createFormPanel();
  var hPanel = app.createHorizontalPanel();
  hPanel.add(app.createLabel('ToDo:'));
  hPanel.add(app.createTextBox().setName('todo'));
  hPanel.add(app.createSubmitButton('保存'));
  formPanel.add(hPanel);
  app.add(formPanel);
  return app;
}
 
// フォームから送信された値をシートに挿入
function doPost(e){
  var app = UiApp.getActiveApplication();
  // To-Do保存先シートのIdからオブジェクト取得
  var ss = SpreadsheetApp.openById('xxxxxxxxxxx');
  var sheet = ss.getSheetByName('シート1');
  sheet.getRange('A'+(sheet.getLastRow()+1)).setValue(e.parameter.todo);
  // doPost()で日本語を出力するとエラーになるため、英数字のみ表示
  app.add(app.createLabel('Saved changes.'));
  return app;
}

この投稿の続きを読む »

[GAS][Ui]リセットボタンを実装するには

リセットボタンを実装

createResetButtonにより、入力された文字列をすべてクリアすることができる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function doGet() {
  var app = UiApp.createApplication();
 
  //フォーム追加
  var formPanel = app.createFormPanel();
  app.add(formPanel);
 
  //フォームレイアウト用パネル作成
  var hPanel = app.createHorizontalPanel();
  formPanel.add(hPanel);  
 
  //テキストボックス、ボタン作成
  var textBox = app.createTextBox();
  var submitButton = app.createSubmitButton('送信');
  var resetButton = app.createResetButton('リセット');
  hPanel.add(textBox)
    .add(submitButton)
    .add(resetButton);
 
  return app; 
 
}

この投稿の続きを読む »

[GAS][Ui]ラベルを挿入するには

挿入したラベル

▼サンプルスクリプト

3つの方法でラベルを挿入している。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function doGet() {
  var app = UiApp.createApplication();
  var vPanel = app.createVerticalPanel();
 
  var label = app.createLabel('Hello Google Apps Scirpt!');
 
  var label2 = app.createLabel();
  label2.setText('ラベルの挿入');  
 
  var label3 = app.createLabel('文字を折り返さない', false)
      .setStyleAttribute('padding', '5px').setStyleAttribute('background', '#bbeeff')
      .setWidth('50px')
 
  vPanel.add(label).add(label2).add(label3);
  app.add(vPanel);
  return app;
}

この投稿の続きを読む »

[GAS][Sites]サイトに含まれる全てのページを取得するには

▼サンプルスクリプト

指定したサイトに含まれるすべてのページを取得し、シートに一覧表示するサンプルスクリプト。

1
2
3
4
5
6
7
8
function listAllPages() {
  var mySite = SitesApp.getSiteByUrl('https://sites.google.com/xxxxxxxxx');
  var pages = mySite.getAllDescendants();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  for(var i=0; i<pages .length; i++){
    sheet.getRange('A'+(sheet.getLastRow()+1)).setValue(pages[i].getTitle());
  }
}

この投稿の続きを読む »

[GAS][スプレッドシート]シートに関数を挿入するには

▼サンプルスクリプト

スプレッドシート関数「GoogleFinance」をシートに挿入するサンプルスクリプト。

1
2
3
4
5
function addFinanceFormula() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange('A'+(sheet.getLastRow()+1))
          .setFormula('=GoogleFinance("GOOG","price")');
}

この投稿の続きを読む »

[GAS][スプレッドシート]シートに値をセットするには

▼サンプルスクリプト

さまざまな方法で値を挿入するサンプルスクリプト。

1
2
3
4
5
6
7
8
9
10
11
12
function insertValue() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 
  sheet.getActiveCell().setValue('選択セルに値をセット');
 
  sheet.getActiveSelection().setValue('複数セルに値セット');
 
  sheet.getRange('A5').setValue('A5に値をセット').setBackgroundColor('#eee');  
 
  sheet.getRange(sheet.getLastRow()+1, sheet.getLastColumn()).
          setValue('最終列の最終行+1に値をセット');
}

この投稿の続きを読む »

[GAS][Ui]標準ボタンを実装するには

標準ボタン

▼サンプルスクリプト

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function doGet() {
  var app = UiApp.createApplication(); 
  var hPanel = app.createHorizontalPanel();
 
  var normalButton = app.createButton('保存');
  var disabledButton = app.createButton('無効').setEnabled(false);
  var buttonWithHtml = app.createButton('<strong>強調</strong>');
  var buttonWithCss = app.createButton('css').
       setStyleAttribute('padding', '10px 10px 10px 10px');
  var buttonWithTitle = app.createButton().
       setTitle('これはボタンです').setText('title属性付き');
 
  hPanel.add(normalButton);
  hPanel.add(disabledButton);
  hPanel.add(buttonWithHtml);
  hPanel.add(buttonWithCss);
  hPanel.add(buttonWithTitle);
 
  app.add(hPanel);
 
  return app;
}

この投稿の続きを読む »