javascript - 无法将 Google 表格作为 TSV 文件访问
问题描述
在过去的几年里,我一直使用表格作为 Web 应用程序的数据源,方法是使用以下代码将 id 转换为 TSV 文件的直接链接:
let id="1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ"
let str="https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv";
var xhr=new XMLHttpRequest();
xhr.open("GET",str);
xhr.onload=function() {/* act on data */ };
xhr.send();
xhr.onreadystatechange=function(e) {
if ((xhr.readyState === 4) && (xhr.status !== 200)) {
/* Show error */
}
它仍然适用于旧文件,但新文件会产生 CORS 错误:
Access to XMLHttpRequest at 'https://doc-00-0g-sheets.googleusercontent.com/export/l5l039s6ni5uumqbsj9o11lmdc/5filqetsf3ohbeiq2e8vbtf8ik/1593267040000/112894833168181755194/*/1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ?format=tsv' (redirected from 'https://docs.google.com/spreadsheets/d/1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ/export?format=tsv') from origin 'https://viseyes.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
作品:www.viseyes.org/scale?1LSnAM3A62AQipZfqxDtlOjt4MWJ0fBP22cdyqJqEj5M
错误:www.viseyes.org/scale?1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ
解决方案
我相信你的目标如下。
- 您想使用 Javascript 从 Google 电子表格中检索 TSV 格式的数据。
- 您的电子表格已公开共享。
为此,这个答案怎么样?
问题和解决方法:
我可以从你的问题中确认同样的情况。不幸的是,我无法删除此错误。因此,在这种情况下,作为一种解决方法,我想建议使用由 Google Apps 脚本创建的 Web 应用程序作为包装器。这样,可以消除错误。此解决方法的流程如下。
- 从 Javascript 请求 Web 应用程序。
- 在 Web 应用程序中,数据是从
"https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv"
. - 从 Web 应用返回 TSV 格式的数据。
用法:
请执行以下流程。
1. 新建 Google Apps Script 项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 Google,则会打开登录屏幕。所以请登录谷歌。至此,Google Apps Script 的脚本编辑器被打开。
2. 准备脚本。
请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。此脚本适用于 Web 应用程序。
function doGet() {
let id = "1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ"; // This is from your script.
let str = "https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv"; // This is from your script.
const value = UrlFetchApp.fetch(str);
return ContentService.createTextOutput(value.getContentText());
}
如果您的 Google 电子表格未公开共享,请进行如下修改。
function doGet() { let id = "1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ"; // This is from your script. let str = "https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv"; // This is from your script. const value = UrlFetchApp.fetch(str, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}}); return ContentService.createTextOutput(value.getContentText()); // DriveApp.getFiles() // This is used for automatically detecting the scope. }
3. 部署 Web 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用程序”打开一个对话框。
- 为“将应用程序执行为:”选择“我” 。
- 这样,脚本作为所有者运行。
- 为“谁有权访问应用程序:”选择“任何人,甚至匿名” 。
- 在这种情况下,不需要请求访问令牌。我认为我为您的目标推荐此设置。
- 当然,您也可以使用访问令牌。届时,请将此设置为“任何人”。并请包括访问令牌的范围
https://www.googleapis.com/auth/drive.readonly
和范围。https://www.googleapis.com/auth/drive
这些范围是访问 Web 应用程序所必需的。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 单击“查看权限”。
- 选择自己的帐户。
- 在“此应用未验证”中单击“高级”。
- 点击“转到###项目名称###(不安全)”
- 单击“允许”按钮。
- 单击“确定”。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec
。- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
4. 使用 Web Apps 运行该功能。
当您使用它时,请按如下方式修改您的 Javascript 脚本并进行测试。
从:let id="1zD3eIL8LCTJ8F_8U3kWA6k5WPJNKr_UZ_93bnARlMxQ"
let str="https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv";
至:
let str = "https://script.google.com/macros/s/###/exec";
笔记:
- 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
- 在我的环境中,我可以确认,当使用上述解决方法时,不会发生错误,并且可以检索 TSV 格式的数据。
参考:
推荐阅读
- c++ - (如何)使用特定格式的单个 'cin' 语句一次输入 2 个值
- python-3.x - 如何使用opencv(python)读取文件夹中的第一张图像
- python - 将变量从类内部的函数中获取到python中的单独类函数中
- sql - 如何根据SQL中的列字段将行转换为SQL中的列
- javascript - ng-select 禁用表单控件不能以反应形式工作
- minikube - 有没有办法使用 skaffold 配置加载私有图像而不构建它?
- python - 修复 nltk 处理文本中的替换引号
- javascript - Javascript-附加多个项目
- c++ - 在一行中连接多个字符串c ++
- c++ - 给定一个未排序的数组,如何删除重复项然后对其进行排序?