LoginSignup
2
1

More than 5 years have passed since last update.

GAS ライブラリー作成② 列・行の操作

Last updated at Posted at 2017-10-04

 ダミーデータ

var D2Arr = [
  ["織田信長",1560,"桶狭間の戦い"],
 ["織田信長",1573,"室町幕府の滅亡"],
  ["織田信長",1582,本能寺の変],
  ["豊臣秀吉",1582,"全国統一"],
  ["徳川家康",1600,"関ヶ原の戦い"],
  ["徳川家康",1615,"大阪夏の陣"]
]

self (列選択)

SELect Fieldの略. 選択したい列番号を配列[]で選択する。

self
    self:
    function (rangeArr){
      var intmp = this.element.concat();        
      var output=[];      

      intmp.forEach(function(row,ridx){
        output[ridx]=[];
        rangeArr.forEach(function(col,cidx){
          output[ridx][cidx]=row[col]
        })
      })

      this.element=output;
      return this;
    } 
結果
D2(D2Arr).self([0,2]).get();
/*[ 
  ["織田信長","桶狭間の戦い"],
 ["織田信長","室町幕府の滅亡"],
  ["織田信長","本能寺の変"],
  ["豊臣秀吉","全国統一"],
  ["徳川家康","関ヶ原の戦い"],
  ["徳川家康","大阪夏の陣"]
] */

delf (列削除)

DELect Fieldの略. 削除したい列番号を配列[]で選択する。

delf
    delf:
    function (rangeArr){
      var intmp = this.element.concat();        
      var output=[];
      // rangeメソッドを受取る
      var range =this.range;
      // flattenした、intmpの要素の数のseq
      var inputRangeArr = Array.prototype.concat.apply([], range(0,intmp[0].length-1));

      // 差集合をもとめる
      var subRangeArr = [];
      inputRangeArr.forEach(function(val,idx){
        if(rangeArr.indexOf(val)  <0){
          subRangeArr.push(val)
        }
      })

      intmp.forEach(function(row,ridx){
        output[ridx]=[];
        subRangeArr.forEach(function(col,cidx){
          output[ridx][cidx]=row[col]
        })
      })

      this.element=output;
      return this;
    }
結果
D2(D2Arr).self([0,2]).delf([0]).get(); // メソッドはいくつでも並べられる
/*[ 
  ["桶狭間の戦い"],
 ["室町幕府の滅亡"],
  ["本能寺の変"],
  ["全国統一"],
  ["関ヶ原の戦い"],
  ["大阪夏の陣"]
] */

selr (行選択)

SELect Recordの略. 選択したい行番号を配列[]で選択する.

selr
    selr:
    function(rangeArr){
      var intmp = this.element.concat();        
      var output=[];

      rangeArr.forEach(function(row,ridx){
        output[ridx]=[];

        intmp.forEach(function(row1,ridx1){
          if(row==ridx1){
            output[ridx] =row1;
          }
        })
      })

      this.element=output;
      return this;
    }
結果
D2(D2Arr).selr([0,1,2]).get(); 
/*[
  ["織田信長",1560,"桶狭間の戦い"],
 ["織田信長",1573,"室町幕府の滅亡"],
  ["織田信長",1582,本能寺の変]
] */

delr (行削除)

DELete Recordの略. 削除したい行番号を配列[]で選択する.
ユニケージはdelrはD2ライブラリーではdelerKeyと等しい.

delr
    delr:
    function(rangeArr){
      var intmp = this.element.concat();        
      var output=[];
      // rangeメソッドを受取る
      var range =this.range;
      // flattenした、intmpの要素の数のseq
      var inputRangeArr = Array.prototype.concat.apply([], range(0,intmp.length-1));

      // 差集合をもとめる
      var subRangeArr = [];
      inputRangeArr.forEach(function(val,idx){
        if(rangeArr.indexOf(val)  <0){
          subRangeArr.push(val)
        }
      })

      subRangeArr.forEach(function(row,ridx){
        output[ridx]=[];

        intmp.forEach(function(row1,ridx1){
          if(row==ridx1){
            output[ridx] =row1;
          }
        })
      })

      this.element=output;
      return this;
    }
結果
D2(D2Arr).delr([0,1,2]).get(); 
/*[
  ["豊臣秀吉",1582,"全国統一"],
  ["徳川家康",1600,"関ヶ原の戦い"],
  ["徳川家康",1615,"大阪夏の陣"]
] */

selrKey (行検索選択)

selrの文字列検索.
第1引数:列番号(ひとつ)
第2引数:検索文字列

selrKey
    selrKey:
    function(fieldNo,searchKey){
      var intmp = this.element.concat();        
      var output=[];

      intmp.forEach(function(row,ridx){
        if(row[fieldNo]==searchKey){
          output.push(row);          
        }
      })

      this.element=output;
      return this;
    }
結果
D2(D2Arr).selrKey(0,"徳川家康").get();
/*[
  ["徳川家康",1600,"関ヶ原の戦い"],
  ["徳川家康",1615,"大阪夏の陣"]
]*/

delrKey (行検索削除)

delrの文字列検索.
第1引数:列番号(ひとつ)
第2引数:検索文字列

delrKey
    delrKey:
    function(fieldNo,searchKey){
      var intmp = this.element.concat();        
      var output=[];

      intmp.forEach(function(row,ridx){
        if(row[fieldNo]!=searchKey){
          output.push(row);          
        }
      })

      this.element=output;
      return this;
    }
結果
D2(D2Arr).selrKey(0,"徳川家康").delrKey(1,1600).get();
/*[
  ["徳川家康",1615,"大阪夏の陣"]
]*/
2
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
2
1