【问题标题】:Use with Excel data to display on Dashing Dashboard?与 Excel 数据一起使用以显示在 Dashing Dashboard 上?
【发布时间】:2018-05-15 23:50:57
【问题描述】:

我正在尝试从github 获取以下代码的示例,这对于我的 Linux/Ubuntu 安装来说似乎是一个死话题。我一直在尝试使用“机械化”从公司内部网中抓取数据,请参阅stack question for details。由于我不够聪明,无法解决我的登录问题,我想我会尝试从 Excel 表中提供数据作为一种解决方法,直到我能弄清楚机械化路线。我又一次不够聪明,无法让提供的代码在 Linux 上运行,因为我收到以下错误:

`kqueue=':此平台不支持 kqueue (EventMachine::Unsupported)

如果我从原始来源中提供的信息中理解正确,那么问题是 Linux 不支持 kqueue。 OP 声明 inotify 是一个替代方案,但我没有找到一个类似的例子,用它在小部件中显示 Excel。

这是 GitHub 上显示的代码,希望帮助将其转换为在 Linux 上工作:

require 'roo'

EM.kqueue = EM.kqueue?
file_path = "#{Dir.pwd}/spreadsheet.xls"

def fetch_spreadsheet_data(path)
  s = Roo::Excel.new(path)
  send_event('valuation',   { current: s.cell(1, 2) })
end

module Handler
  def file_modified
    fetch_spreadsheet_data(path)
  end
end

fetch_spreadsheet_data(file_path)

EM.next_tick do
  EM.watch_file(file_path, Handler)
end

【问题讨论】:

    标签: ruby linux excel dashing roo-gem


    【解决方案1】:

    好的,所以我能够通过执行以下操作使其正常工作并在 Dashing Dashboard 小部件上显示我的数据:

    首先:我已将 spreadsheet.xls 上传到仪表板的根目录。

    第二:我将/jobs/sample.rb代码替换为:

    #!/usr/bin/env ruby
    require 'roo'
    
    
    SCHEDULER.every '2s' do
    file_path = "#{Dir.pwd}/spreadsheet.xls"
    
    def fetch_spreadsheet_data(path)
      s = Roo::Excel.new(path)
      send_event('valuation',   { current: s.cell('B',49) })
    
    end
    
    module Handler
      def file_modified
        fetch_spreadsheet_data(path)
      end
    end
    
    fetch_spreadsheet_data(file_path)
    
    end
    

    第三:确保 /widgets/number 在您的仪表板中“这是示例安装的一部分”。

    第四:将以下代码添加到您的 /dashboards/sample.erb 文件“这也是示例安装的一部分”。

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
          <div data-id="valuation" data-view="Number" data-title="Current Valuation" data-prefix="$"></div>
        </li>
    

    我使用this source 来帮助我更好地了解 Roo 的工作原理。我通过更改我的值并将电子表格.xls 重新上传到服务器来测试我的小部件,并在我的仪表板上看到即时更改。

    希望这对某人有所帮助,我仍在寻求帮助以通过抓取数据来自动化此过程。参考this if you can help

    【讨论】:

      【解决方案2】:

      感谢您分享此代码示例。我没有设法让它在我的环境(Raspberry/Raspbian)中工作,但经过一些努力,我设法想出了一些可行的东西——至少对我来说是这样;)

      这周之前我从未使用过 Ruby,所以这段代码可能有点糟糕。请接受道歉。

      -- 克里斯托夫

      require 'roo'
      require 'rubygems'
      require 'rb-inotify'
      
      # Implement INotify::Notifier.watch as described here:
      # https://www.go4expert.com/articles/track-file-changes-ruby-inotify-t30264/
      
      file_path = "#{Dir.pwd}/datasheet.csv"
      
      def fetch_spreadsheet_data(path)
        s = Roo::CSV.new(path)
        send_event('csvdata',   { value: s.cell(1, 1) })
      end
      
      SCHEDULER.every '5s' do
      
      notifier = INotify::Notifier.new
      
      notifier.watch(file_path, :modify) do |event|
          event.flags.each do |flag|
              ## convert to string
              flag = flag.to_s
      
              puts case flag
                  when 'modify' then fetch_spreadsheet_data(file_path)
              end
          end
      end
      
      ## loop, wait for events from inotify
      notifier.process
      
      end
      

      【讨论】:

        猜你喜欢
        • 2017-02-01
        • 1970-01-01
        • 2017-08-28
        • 2021-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多