从没有 Sheets API 的 Google Sheets 获取数据 [英] Get data from Google Sheets without Sheets API

查看:34
本文介绍了从没有 Sheets API 的 Google Sheets 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用(React Native),我需要在其中显示谷歌表单的回复,所以我创建了回复表.

I have created an app (React Native) where I am required to show the responses of a google form, so I created the Responses Sheet.

虽然 Sheets API 是最好的解决方案,但我不想使用它(除非别无选择).

Though Sheets API would be the best solution, I do not wish to use it (unless there is no other choice).

有没有办法在不使用 Google API 的情况下从工作表中获取数据(csv/json/arrays?/其他)?我只想要单元格中的值,别无其他.

Is there a way to get the data (csv/json/arrays?/anything else) from the sheet without using Google API? I only want the values from the cells, nothing else.

注意事项:

  • 我想用 JavaScript 解析数据.

推荐答案

我相信你的目标如下.

  • 您想在不使用 Sheets API 的情况下从 Google 电子表格中检索值.
    • 我了解在这种情况下,您希望在不使用访问令牌和 API 密钥的情况下检索值.

    当 Google 电子表格与 Sheets API 一起使用时,需要使用访问令牌和 API 密钥.当您想在不使用访问令牌和 API 密钥的情况下从 Google 电子表格中检索值时,在此答案中,我想提出以下使用由 Google Apps 脚本创建的 Web 应用程序的解决方法.此变通方法的流程如下.

    When Google Spreadsheet is used with Sheets API, the access token and the API key are required to be used. When you want to retrieve the values from Google Spreadsheet without using the access token and the API key, in this answer, I would like to propose the following workaround using Web Apps created by Google Apps Script. The flow of this workaround is as follows.

    1. 从 Javascript 请求网络应用.
    2. 在 Web Apps 中,这些值是从 Google 电子表格返回的.
      • 在此变通方法中,不需要公开共享 Google 电子表格.

    用法:

    请执行以下流程.

    Usage:

    Please do the following flow.

    Web Apps 的示例脚本是 Google Apps 脚本.所以请创建一个 Google Apps Script 项目.

    Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.

    如果您想直接创建它,请访问https://script.new/.在这种情况下,如果您未登录 Google,则会打开登录屏幕.所以请登录谷歌.这样,Google Apps Script 的脚本编辑器就打开了.

    If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.

    请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中.此脚本适用于 Web 应用程序.

    Please copy and paste the following script (Google Apps Script) to the script editor. This script is for the Web Apps.

    function doGet(e) {
      const id = e.parameter.spreadsheetId;
      const sheetName = e.parameter.sheetName;
      const sheet = SpreadsheetApp.openById(id).getSheetByName(sheetName);
      const values = sheet.getDataRange().getValues();
      return ContentService.createTextOutput(JSON.stringify({values: values})).setMimeType(ContentService.MimeType.JSON);
    }
    

    3.部署网络应用.

    1. 在脚本编辑器上,通过发布"打开一个对话框->部署为 Web 应用程序".
    2. 选择我"作为执行应用程序:".
      • 由此,脚本以所有者身份运行.
    • 在这种情况下,不需要请求访问令牌.我认为我推荐使用此设置来实现您的目标.
    • 当然,您也可以使用访问令牌.届时,请将其设置为任何人".并且请将 https://www.googleapis.com/auth/drive.readonlyhttps://www.googleapis.com/auth/drive 的范围包含到访问令牌.这些范围是访问网络应用所必需的.
    • In this case, no access token is required to be request. I think that I recommend this setting for your goal.
    • Of course, you can also use the access token. At that time, please set this to "Anyone". And please include the scope of https://www.googleapis.com/auth/drive.readonly and https://www.googleapis.com/auth/drive to the access token. These scopes are required to access to Web Apps.
    1. 点击查看权限".
    2. 选择自己的帐户.
    3. 点击高级"在此应用未经验证".
    4. 点击转到###项目名称###(不安全)"
    5. 点击允许"按钮.

  • 点击确定".
  • 复制 Web 应用程序的 URL.这就像 https://script.google.com/macros/s/###/exec.
    • 当您修改 Google Apps 脚本时,请重新部署为新版本.这样,修改后的脚本就会反映到 Web Apps 中.请注意这一点.
    • 4.使用网络应用程序运行该函数.

      您可以使用以下脚本从 Google 电子表格中检索值.

      4. Run the function using Web Apps.

      You can retrieve the values from Google Spreadsheet using the following script.

      const baseUrl = "https://script.google.com/macros/s/###/exec";  // Please set your Web Apps URL.
      const para = {
        spreadsheetId: "###",  // Please set your Google Spreadsheet ID.
        sheetName: "Sheet1"  // Please set the sheet name you want to retrieve the values.
      };
      const q = new URLSearchParams(para);
      const url = baseUrl + "?" + q;
      fetch(url)
        .then(res => res.json())
        .then(res => {
          const values = res.values;
          console.log(values);
        });
      

      如果您想使用 curl 命令测试上述 Web Apps,您也可以使用以下 curl 命令.

      If you want to test the above Web Apps using a curl command, you can also use the following curl command.

      $ curl -L "https://script.google.com/macros/s/###/exec?spreadsheetId=###&sheetName=Sheet1"
      

      注意:

      • 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本.这样,最新的脚本就会反映到 Web Apps 中.请注意这一点.
      • 在我的环境中,最近,我确认当使用 Javascript 从公开共享的电子表格和发布的电子表格检索值到 Web 时,会发生与 CORS 相关的错误.我担心这样的问题也可能发生在您的环境中.所以为了实现你的目标,我提议使用 Web Apps.在这种情况下,我已经确认没有发生错误.
      • 这篇关于从没有 Sheets API 的 Google Sheets 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆