Class: EasyGoogleDrive::Spreadsheet

Inherits:
Drive
  • Object
show all
Defined in:
lib/easy-google-drive/spreadsheet.rb

Constant Summary

Constants inherited from Drive

Drive::APPLICATION_NAME, Drive::CLIENT_SECRETS_PATH, Drive::CREDENTIALS_PATH, Drive::OOB_URI, Drive::SCOPE

Instance Method Summary collapse

Methods inherited from Drive

#authorize, #cd, #directory, #get, #get_folderid, #initialize, #list, #list_files, #ls, #mkdir, #pwd, #rm, #rmdir, #root, #send, #shared

Constructor Details

This class inherits a constructor from EasyGoogleDrive::Drive

Instance Method Details

#addNewLine(sheet, data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/easy-google-drive/spreadsheet.rb', line 72

def addNewLine(sheet,data)

  request_body = Google::Apis::SheetsV4::ValueRange.new
  spreadsheet_id = @spreadsheet[:file].id
  if sheet == "" then
    range = "A:"+("A".ord + data.length).chr
  else
    range = sheet
  end

  value_range_object = {
    majorDimension:"ROWS",
    values: [data],
  }
  update_res = @spreadsheet[:service].append_spreadsheet_value(spreadsheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
  return update_res
end

#closeObject



99
100
101
# File 'lib/easy-google-drive/spreadsheet.rb', line 99

def close()
  initialize()
end

#getData(sheet, range) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/easy-google-drive/spreadsheet.rb', line 89

def getData(sheet,range)
  if sheet == "" then
    get_range = range
  else
    get_range = sheet + "!" + range
  end
  spreadsheet_id = @spreadsheet[:file].id
  response = @spreadsheet[:service].get_spreadsheet_values(spreadsheet_id, get_range)
  return response.values
end

#helpObject



102
103
# File 'lib/easy-google-drive/spreadsheet.rb', line 102

def help()
end

#init_sheet_apiObject



65
66
67
68
69
70
71
# File 'lib/easy-google-drive/spreadsheet.rb', line 65

def init_sheet_api()
  service = Google::Apis::SheetsV4::SheetsService.new
  service.client_options.application_name = APPLICATION_NAME
  service.authorization = authorize
  spreadsheet_id = @spreadsheet[:file].id
  @spreadsheet[:service] = service
end

#open(file) ⇒ Object

SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY application/vnd.google-apps.spreadsheet Google Sheets



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy-google-drive/spreadsheet.rb', line 11

def open(file)
  tmp_list = []
  tmp_path = []
  tmp_path.push(@root_path.last)
  list = list_files(file,tmp_path,tmp_list)
  if list.length == 1 and list[0].mime_type == "application/vnd.google-apps.spreadsheet" then
    @spreadsheet = {
      file: list[0],
      opened: true,
    }
    init_sheet_api()
  elsif list.length == 1 and list[0].mime_type != "application/vnd.google-apps.spreadsheet" then
    puts "file type of "+ file + "is not spreadsheet."
    puts "the file type is " + spreadsheet_file.mime_type + "."
    @spreadsheet = {}
    return true
  elsif list.length > 1 then
    puts "find "+ list.length + "files."
    @spreadsheet = {}
    return false
  else
    puts "cannot find file. Do you create ? (Yes/No)"
    begin
      data = gets.chop
      if ["Yes","Y","yes","y","YES"].find {|n| n == data}
        continue = false
        # create new file
        name = file.split("/").last
        parents = [tmp_path.last[:id]]
         = {
          name: name,
          parents: parents,
          mime_type: 'application/vnd.google-apps.spreadsheet',
        }
        @service.create_file(, fields: 'id')
        #get file id
        list = list_files(name,tmp_path,tmp_list)
        @spreadsheet = {
          file: list[0],
          opened: true,
        }
        puts "success to create file:: " + name
        init_sheet_api()
      elsif ["No","N","No","n","NO"].find {|n| n == data}
        continue = false
        data = "no"
        @spreadsheet = {}
      else
        puts "cannot find file. Do you create ? (Yes/No)"
        continue = true
      end
    end while(continue)
  end
end