首页 > 解决方案 > 无法将 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

标签: javascriptgoogle-sheets-apigoogle-docs-api

解决方案


我相信你的目标如下。

  • 您想使用 Javascript 从 Google 电子表格中检索 TSV 格式的数据。
  • 您的电子表格已公开共享。

为此,这个答案怎么样?

问题和解决方法:

我可以从你的问题中确认同样的情况。不幸的是,我无法删除此错误。因此,在这种情况下,作为一种解决方法,我想建议使用由 Google Apps 脚本创建的 Web 应用程序作为包装器。这样,可以消除错误。此解决方法的流程如下。

  1. 从 Javascript 请求 Web 应用程序。
  2. 在 Web 应用程序中,数据是从"https://docs.google.com/spreadsheets/d/"+id+"/export?format=tsv".
  3. 从 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 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用程序”打开一个对话框。
  2. “将应用程序执行为:”选择“我” 。
    • 这样,脚本作为所有者运行。
  3. “谁有权访问应用程序:”选择“任何人,甚至匿名” 。
    • 在这种情况下,不需要请求访问令牌。我认为我为您的目标推荐此设置。
    • 当然,您也可以使用访问令牌。届时,请将此设置为“任何人”。并请包括访问令牌的范围https://www.googleapis.com/auth/drive.readonly和范围。https://www.googleapis.com/auth/drive这些范围是访问 Web 应用程序所必需的。
  4. 单击“部署”按钮作为新的“项目版本”。
  5. 自动打开“需要授权”对话框。
    1. 单击“查看权限”。
    2. 选择自己的帐户。
    3. 在“此应用未验证”中单击“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 单击“允许”按钮。
  6. 单击“确定”。
  7. 复制 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 格式的数据。

参考:


推荐阅读