spreadsheets.values.get,但与其他工作表一起使用



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

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

我相信你的目标如下。

  • 您想要从Sheet2表格中的单元格A1:D1中检索值
  • 您希望使用spreadsheets.values.get方法在Sheetsneneneba API中使用Javascript(不是Node.js(来实现这一点
  • 您已经能够使用Sheets API从Google电子表格中获取值

对此,这个答案如何?

模式1:

在该模式中,使用了该官方文档的示例脚本(Browser(的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-...不同。所以请小心。请查收这份正式文件
  • 在这种情况下,假设您已经完成了使用Sheetsneneneba API的授权过程

模式2:

在这种模式中,将使用类似https://docs.google.com/spreadsheets/d/e/2PACX-...的URL。根据您问题中的URL,可以发现电子表格已发布到Web上。在这种情况下,只有从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=######

参考文献:

  • 电子表格ID
  • 工作表ID