ruby-on-rails - 在 Ruby 中打开并读取 Google Drive 中的文件

标签 ruby-on-rails ruby ruby-on-rails-4 google-api rubygems

我正在使用 Google Drive API,它为我提供了“导出链接”,这些链接似乎会重定向以下载文档。

结果如下:

{"text/csv":"https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxxxxxx-rI&exportFormat=csv",
"application/pdf":"https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxxxxxx-rI&exportFormat=pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":"https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxxxxxx-rI&exportFormat=xlsx"}

我正在使用Yomu但是当我尝试读取这些文件之一时,我得到的结果表明“内容已移动”,因此是某种重定向。

我如何打开这些网址之一以便 Yomu 可以读取它们,或者您是否知道使用 Google Drive API 来获取所需数据的任何替代方法?

最佳答案

我可以使用curl加载该文件,而且它似乎工作得很好。顺便说一句,如果您想保持文档的私密性,您需要屏蔽 PDF 和 XLSX URL 中的 id。此外,CSV 比 XLSX 文件更容易使用。

在你的 shell 中尝试

curl "https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxx-rI&exportFormat=csv"

尝试在 IRB/rails 控制台 session 中:

`curl "https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxx-rI&exportFormat=csv"`

require 'open-uri'
open('https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxx-rI&exportFormat=csv').read

require 'csv'
require 'open-uri'
CSV.parse(open('https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxx-rI&exportFormat=csv'))

如果您确实想使用完整的 XLSX:

require 'yomu'
Yomu.new('https://docs.google.com/spreadsheets/export?id=1w6oq05R8piP4N4A-xxxx-rI&exportFormat=xlsx').text

但是结果看起来非常难以解析。

关于ruby-on-rails - 在 Ruby 中打开并读取 Google Drive 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248077/

相关文章:

javascript - raphael.2.1.0.min.js 具有无效的 UTF-8 字节序列

ruby-on-rails - 如何设置本地 SSL 证书和 Rails 应用程序?

ruby-on-rails - 如何使用 nginx 设置从 http 到 https 的重定向?

ruby - 从 Ruby 哈希数组中提取数据

c - 为什么我得到 "Header file missing"(出错)?

ruby-on-rails - Rails 通过连接按字符串列值排序

ruby-on-rails - Eventmachine 因段错误而失败

ruby-on-rails - Ruby On Rails 的默认 respond_to 格式是什么以及如何添加新格式?

ruby - ASCII SHA1 哈希 Ruby

ruby-on-rails - 在 Rails 模型中使用数组作为数据类型时,在表单字段中我得到空大括号 `{}`