Sha256: 41a53aa2465ce1a80185be7d412ab6e65d693cef73a2ae84617230620889fd90

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require "google/api_client"
require "google_drive"

module MilkMaid
  TIMESTAMP_COL = 1
  READING_COL = 2
  DATA_COL = 3

  class SpreadsheetWriter
    def self.write_spreadsheet(session_file, spreadsheet_name, record)
      session = GoogleDrive.saved_session(session_file)

      puts "Creating Spreadsheet #{spreadsheet_name}"
      sheet = session.create_spreadsheet(spreadsheet_name)

      print_record(sheet, record)
    end

    def self.print_footer(ws, row, record)
      ws[row, 1] = "Batch Name"
      ws[row, 2] = record.name
      row += 1
      ws[row, 1] = "Duration"
      ws[row, 2] = record.duration
      row += 1
      ws[row, 1] = "Base Temperature"
      ws[row, 2] = record.base_temperature
      row += 1
      ws[row, 1] = "Status"
      ws[row, 2] = record.status
      ws.save
    end

    def self.print_record(sheet, record)
      worksheet_name = "#{record.name}: #{record.status}"
      puts "Creating worksheet for #{worksheet_name}"
      ws = sheet.add_worksheet(worksheet_name)

      ws[1,TIMESTAMP_COL] = "Timestamp"
      ws[1,READING_COL] = "Reading"
      ws[1,DATA_COL] = "Value"

      row = 2

      record.events.each do |event|
        ws[row, TIMESTAMP_COL] = event.timestamp
        ws[row, READING_COL] = event.name
        ws[row, DATA_COL] = event.data unless event.data == 0
        row += 1
      end

      print_footer(ws, row + 1, record)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milk_maid-0.6.1 lib/milk_maid/spreadsheet_writer.rb