![](/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.