![](/img/trans.png)
[英]Exporting Data from Google Sheets using Google Sheets API with Node.js
[英]Get data from Google Sheets without Sheets API
我创建了一个应用程序(React Native),我需要在其中显示谷歌表单的响应,所以我创建了响应表。
虽然 Sheets API 是最好的解决方案,但我不想使用它(除非没有其他选择)。
有没有办法在不使用 Google API 的情况下从工作表中获取数据(csv/json/arrays?/其他任何东西)? 我只想要单元格中的值,没有别的。
笔记:
我相信你的目标如下。
当 Google 电子表格与表格 API 一起使用时,需要使用访问令牌和 API 密钥。 当您想在不使用访问令牌和 API 键的情况下从 Google 电子表格中检索值时,在此答案中,我想使用由 Google Apps 脚本创建的 Web 应用程序提出以下解决方法。 此解决方法的流程如下。
请执行以下流程。
Web Apps 的示例脚本是 Google Apps 脚本。 所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/ 。 在这种情况下,如果您没有登录 Google,则会打开登录屏幕。 所以请登录谷歌。 至此,Google Apps Script 的脚本编辑器被打开。
请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。 此脚本适用于 Web 应用程序。
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);
}
https://www.googleapis.com/auth/drive.readonly
和https://www.googleapis.com/auth/drive
的 scope。 这些范围是访问 Web 应用程序所必需的。https://script.google.com/macros/s/###/exec
。
您可以使用以下脚本从 Google 电子表格中检索值。
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 应用程序,还可以使用以下 curl 命令。
$ curl -L "https://script.google.com/macros/s/###/exec?spreadsheetId=###&sheetName=Sheet1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.