Les étapes pour accéder à la feuille de calcul ne sont pas décrites ici. Il résume les processus fréquemment utilisés lors de la création de divers outils.
sheet.rb
class Sheet
    #Initialisez en passant l'identifiant de la feuille
    # sheet = Sheet.new('xxxxxxxxxxxxxxxxxx')
    #Utilisez comme
    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
    #Définir la feuille à utiliser lors de la lecture et de l'écriture
    # sheet = Sheet.new('xxxxxxxxxxxxxxxxxx')
    # sheet.setWorksheet('Feuille 1')
    #Utilisez comme
    def setWorksheet(sheet_name)
        @ws = worksheet(sheet_name.to_s)
    end
    #Vérifiez si la feuille existe
    # exist_sheet = sheet.isExistWorksheet('Feuille 1')
    #Utilisez comme
    def isExistWorksheet(sheet_name)
        if self.worksheet(sheet_name).nil? then
            return false
        end
        return true
    end
    #Hash lors de l'utilisation d'une feuille comme une configuration ou une base de données_key_données avec arr comme clé_Faire tout le hachage de la liste
    # 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'}
    # ]
    #Ça revient comme ça.
    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()
    #Équivalent à.
    # start_row,start_Si col est nul, 1 et fin respectivement_row,end_num si col est nul_rows,num_Obtenez comme 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_écrire comme cellule pour commencer col_Ecrire avec des données
    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
    #Obtenez le nombre de dernières lignes
    def getLastRow
        @ws.num_rows
    end
    #Obtenez le nombre de dernières colonnes
    def getLastCol
        @ws.num_cols
    end
    #Obtenir la feuille de calcul par titre
    def worksheet(title)
        @sheet.worksheet_by_title(title)
    end
    #Créé en spécifiant un nom
    def add_worksheet(title, rows, cols)
        @sheet.add_worksheet(title, max_rows = rows, max_cols = cols)
    end
    #Faire une copie
    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
Recommended Posts