【问题标题】:spreadsheets.values.get but with other sheets电子表格.values.get 但与其他工作表
【发布时间】:2020-04-25 13:25:11
【问题描述】:

所以我试图从 JS(不是 Node)中的电子表格中读取值,我遇到了https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get。这看起来对我来说是正确的,但是如果我试图从不同的工作表中读取值怎么办?

例如,我将如何使用电子表格.values.get 来读取 https://docs.google.com/spreadsheets/d/e/2PACX-1vQLrcAW_bL5HSVHorBJisdwv8S5_6Th9EP2wiYLmJKktj41uXVepUNOx4USNGdsVAuDOH_qknWs3pGa/pubhtml 表 2 中的 A1:D1 还是我错过了另一种方法?

【问题讨论】:

    标签: javascript google-sheets google-sheets-api


    【解决方案1】:

    我相信你的目标如下。

    • 您想从Sheet2 工作表中的单元格A1:D1 中检索值。
    • 您希望使用 Sheets API 中的电子表格.values.get 方法和非 Node.js 的 Javascript 来实现此目的。
    • 您已经能够使用 Sheets API 从 Google 电子表格中获取值。

    对于这个,这个答案怎么样?

    模式一:

    在这个模式中,使用了this official document的示例脚本(浏览器)的makeApiCall()函数。

    示例脚本:

    var params = {
      spreadsheetId: '###',  // Please set the Spreadsheet ID.
      range: 'Sheet2!A1:D1',  // Please set the range as a1Notation.
    };
    var request = gapi.client.sheets.spreadsheets.values.get(params);
    request.then(function(response) {
      console.log(response.result.values);
    }, function(reason) {
      console.error('error: ' + reason.result.error.message);
    });
    
    • 电子表格 ID 与 2PACX-... 不同。所以请注意这一点。请查看this official document
    • 在这种情况下,假设您已经完成了使用 Sheets API 的授权过程。

    模式 2:

    在此模式中,使用您的 URL,例如 https://docs.google.com/spreadsheets/d/e/2PACX-...。从您问题中的 URL 可以发现,电子表格已发布到 Web。在这种情况下,只有从网络发布的电子表格中检索到值时,您还可以使用以下脚本。

    示例脚本:

    const url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQLrcAW_bL5HSVHorBJisdwv8S5_6Th9EP2wiYLmJKktj41uXVepUNOx4USNGdsVAuDOH_qknWs3pGa/pub?gid=###&range=A1%3AD1&output=csv";
    fetch(url).then(res => res.text()).then(txt => console.log(txt));
    
    • 在这种情况下,请将工作表 ID 设置为 ###gid=###

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2014-08-23
      相关资源
      最近更新 更多