如何通过Google App Maker访问Google Drive表格数据?创建应用后无法查看示例数据
Hey there! Let's figure out how to get your app to access the data stored in your Google Drive spreadsheet—this is a common hiccup, so we’ll walk through it step by step.
How to Access Your Google Drive Spreadsheet Data
First, let's start with the most likely culprit: permission settings. If your app can't see the data you manually entered, chances are the spreadsheet isn't shared properly for your app to access it.
Step 1: Double-Check Sharing Permissions
- Open your Google Drive spreadsheet.
- Hit the Share button in the top-right corner.
- Adjust settings based on your app type:
- If you're using a service account (for backend apps), add the service account's email address as an editor or viewer.
- For frontend apps or public access (use this carefully!), set sharing to "Anyone with the link can view" (or edit if your app needs to write data). Avoid "Public on the web" unless absolutely necessary.
Step 2: Use the Google Sheets API (Recommended for Custom Apps)
The Google Sheets API is the most reliable way to pull data into your app. Here's a quick setup:
- Enable the API in Google Cloud Console
- Head to your Google Cloud project, search for "Google Sheets API", and enable it.
- Create credentials: Use an API key for public data, or OAuth 2.0 credentials if you need user-specific access.
- Fetch Data with a Simple Request
Here's a JavaScript example for frontend apps:
Just swap in your actual API key and spreadsheet ID, and make sure the sharing settings match your credential type.const API_KEY = 'YOUR_API_KEY'; const SPREADSHEET_ID = 'YOUR_SPREADSHEET_ID'; // Grab this from your spreadsheet's URL const DATA_RANGE = 'Sheet1!A1:Z'; // Adjust to cover your data fetch(`https://sheets.googleapis.com/v4/spreadsheets/${SPREADSHEET_ID}/values/${DATA_RANGE}?key=${API_KEY}`) .then(res => res.json()) .then(data => { console.log('Your spreadsheet data:', data.values); // Now you can render this data in your app's UI }) .catch(err => console.error('Oops, error fetching data:', err));
Step 3: Alternative - Google Apps Script for Server-Side Access
If you're building something integrated with Google Workspace, or want to avoid the full API setup, use Apps Script:
- Open your spreadsheet, go to Extensions > Apps Script.
- Write a simple function to pull your data:
function getSheetData() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); return sheet.getDataRange().getValues(); // Gets all rows/columns with data } - Deploy this as a web app: Set access to "Anyone, even anonymous" (if needed) and copy the web app URL.
- Call this URL from your app to fetch the data directly.
Quick Troubleshooting Tips
- Permission errors? Double-check that the email linked to your app's credentials has access to the spreadsheet.
- Missing data? Ensure your
DATA_RANGEcovers all the rows/columns where you entered data—fixed ranges might miss new entries. - OAuth issues? Make sure your OAuth consent screen is properly set up, and you're requesting the right scopes (like
https://www.googleapis.com/auth/spreadsheets.readonlyfor read access).
内容的提问来源于stack exchange,提问作者Pratik Thacker




