我正在使用 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/