Fun*_*net 1 javascript get google-sheets google-apps-script
我创建了一个应用程序(React Native),需要在其中显示谷歌表单的响应,因此我创建了响应表。
虽然 Sheets API 将是最好的解决方案,但我不想使用它(除非没有其他选择)。
有没有办法在不使用 Google API 的情况下从工作表中获取数据(csv/json/arrays?/其他任何内容)?我只想要单元格中的值,不需要其他任何东西。
笔记:
我相信你的目标如下。
当 Google Spreadsheet 与 Sheets API 一起使用时,需要使用访问令牌和 API 密钥。当您想在不使用访问令牌和 API 密钥的情况下从 Google 电子表格中检索值时,在本答案中,我想建议使用由 Google Apps 脚本创建的 Web Apps 进行以下解决方法。此解决方法的流程如下。
请执行以下流程。
Web Apps 的示例脚本是 Google Apps 脚本。因此,请创建一个 Google Apps 脚本项目。
如果您想直接创建,请访问https://script.new/。在这种情况下,如果您尚未登录 Google,则会打开登录屏幕。所以请登录谷歌。这样,Google Apps 脚本的脚本编辑器就打开了。
请将以下脚本(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);
}
Run Code Online (Sandbox Code Playgroud)
https://www.googleapis.com/auth/drive.readonly和范围。https://www.googleapis.com/auth/drive访问 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);
});
Run Code Online (Sandbox Code Playgroud)
如果您想使用curl命令测试上述Web Apps,也可以使用以下curl命令。
$ curl -L "https://script.google.com/macros/s/###/exec?spreadsheetId=###&sheetName=Sheet1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3266 次 |
| 最近记录: |