1
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?

More than 3 years have passed since last update.

SpreadSheetで頻繁に使う処理

Posted at

#はじめに
ここではスプレッドシートへアクセスできるようになるまでの手順は説明しません。
様々なツールを作成する時に頻繁に使う処理をまとめています。

sheet.rb
class Sheet
    # シートのidを渡して初期化
    # sheet = Sheet.new('xxxxxxxxxxxxxxxxxx')
    # のように使用します
    def initialize(sheet_key)
        @sheet_key = sheet_key
        @config_file = "path/to/google_drive_config.json"
        @session = GoogleDrive::Session.from_config(@config_file)
        begin
            @sheet = @session.spreadsheet_by_key(@sheet_key)
        rescue => e
            puts e.message
        end
        @ws = nil
    end

    # 読み書きする時に使用するシートを設定します
    # sheet = Sheet.new('xxxxxxxxxxxxxxxxxx')
    # sheet.setWorksheet('シート1')
    # のように使用します
    def setWorksheet(sheet_name)
        @ws = worksheet(sheet_name.to_s)
    end

    # シートが存在するか確認します
    # exist_sheet = sheet.isExistWorksheet('シート1')
    # のように使用します
    def isExistWorksheet(sheet_name)
        if self.worksheet(sheet_name).nil? then
            return false
        end
        return true
    end

    # シートをコンフィグやDBのように使用する時にhash_key_arrをキーとしてdata_listを全てhashにします
    # hash_key_arr = [id, value, created_at]
    # data_list = [[1, 'hoge', '2020-01-01'], [2, 'hogehoge', '2020-01-02']]
    # を
    # [
    #    {id: 1, value: 'hoge', created_at: '2020-01-01'},
    #    {id: 2, value: 'hogehoge', created_at: '2020-01-02'}
    # ]
    # のようにして返します。
    def createHash(hash_key_arr, data_list)
        ret = []
        data_list.each{|data|
            tmp = {}
            hash_key_arr.each_with_index{|hash,index|
                tmp[hash] = data[index]
            }
            ret.push(tmp)
        }
        return ret
    end

    # getRange(start_row, start_col, end_row - start_row, end_col - start_col).getValues()
    # と同等。
    # start_row,start_colはnilであればそれぞれ1、 end_row,end_colはnilであればnum_rows,num_colsとして取得します。
    def getData(start_row=nil, end_row=nil, start_col=nil, end_col=nil)
        ret = []
        if start_col.nil? then
            start_col = 1
        end
        if end_col.nil? then
            end_col = @ws.num_cols
        end
        if start_row.nil? then
            start_row = 1
        end
        if end_row.nil? then
            end_row = @ws.num_rows
        end
        for row_index in start_row..end_row do
            data = []
            for col_index in start_col..end_col do
                data.push(@ws[row_index, col_index])
            end
            ret.push(data)
        end
        return ret
    end

    # start_row,start_colを開始するセルとしてwrite_datasで書き込みます
    def writeData(start_row, start_col, write_datas)
        write_datas.each_with_index{|rows, row_index|
            rows.each_with_index{|col, col_index|
                @ws[start_row + row_index, start_col + col_index] = col
            }
        }
        @ws.save
    end

    # 最後の行数を取得
    def getLastRow
        @ws.num_rows
    end

    # 最後の列数を取得
    def getLastCol
        @ws.num_cols
    end

    # worksheetをタイトルで取得
    def worksheet(title)
        @sheet.worksheet_by_title(title)
    end

    # 名前を指定して作成
    def add_worksheet(title, rows, cols)
        @sheet.add_worksheet(title, max_rows = rows, max_cols = cols)
    end

    #コピーを作成
    def copy_worksheet(base, title)
        num_rows = 1000
        num_cols = 14
        new_sheet = add_worksheet(title, num_rows, num_cols)
        origin_data = worksheet(base)
        num_rows = origin_data.num_rows
        num_cols = origin_data.num_cols
        for row_index in 1..num_rows do
            for col_index in 1..num_cols do
                new_sheet[row_index, col_index] = origin_data[row_index, col_index]
            end
        end
        new_sheet.save
    end
end

#参考
https://qiita.com/koshilife/items/4baf1804c585690fc295

1
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
1
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?