Google Drive Picker代码运行报错:DriveApp is not defined问题求助
Hey there! Let's break down why you're hitting that DriveApp is not defined error and how to fix it quickly.
The Root Cause
Your getOAuthToken function that uses DriveApp.getRootFolder() is sitting inside the HTML file's <script> tag. That's a problem because DriveApp is a server-side Google Apps Script service—it only works in .gs files, not in the browser's frontend JavaScript environment. The browser doesn't recognize these Google-specific services, hence the error.
The Fix: Split Your Code into Server-Side and Frontend Files
Google Apps Script projects work best when you separate server-side logic (in .gs files) from frontend HTML/CSS/JS. Here's how to restructure your code correctly:
1. Create a Code.gs File (Server-Side Logic)
This is where you'll put functions that interact with Google services like DriveApp and SpreadsheetApp:
function showPicker() { var html = HtmlService.createHtmlOutputFromFile('PickerHTML2.html') .setWidth(600) .setHeight(425) .setSandboxMode(HtmlService.SandboxMode.IFRAME); SpreadsheetApp.getUi().showModalDialog(html, 'Select a file'); } function getOAuthToken() { DriveApp.getRootFolder(); // This triggers the required OAuth permissions return ScriptApp.getOAuthToken(); }
2. Create a PickerHTML2.html File (Frontend UI)
This file handles the picker UI and client-side logic, calling the server-side function via google.script.run:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css"> <script> // IMPORTANT: Replace this with your actual API key from Google Developers Console var DEVELOPER_KEY = 'your-api-key-here'; var DIALOG_DIMENSIONS = {width: 600, height: 425}; var pickerApiLoaded = false; function onApiLoad() { gapi.load('picker', {'callback': function() { pickerApiLoaded = true; }}); } // Frontend function that calls the server-side getOAuthToken function getOAuthToken() { google.script.run.withSuccessHandler(createPicker) .withFailureHandler(showError).getOAuthToken(); } function createPicker(token) { var uploadView = new google.picker.DocsUploadView().setIncludeFolders(true); if (pickerApiLoaded && token) { var picker = new google.picker.PickerBuilder() .addView(uploadView) .enableFeature(google.picker.Feature.NAV_HIDDEN) .hideTitleBar() .setOAuthToken(token) .setDeveloperKey(DEVELOPER_KEY) .setCallback(pickerCallback) .setOrigin(google.script.host.origin) .setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2) .build(); picker.setVisible(true); } else { showError('Unable to load the file picker.'); } } function pickerCallback(data) { var action = data[google.picker.Response.ACTION]; if (action == google.picker.Action.PICKED) { var doc = data[google.picker.Response.DOCUMENTS][0]; var id = doc[google.picker.Document.ID]; var url = doc[google.picker.Document.URL]; var title = doc[google.picker.Document.NAME]; document.getElementById('result').innerHTML = '<b>You chose:</b><br>Name: <a href="' + url + '">' + title + '</a><br>ID: ' + id; } else if (action == google.picker.Action.CANCEL) { document.getElementById('result').innerHTML = 'Picker canceled.'; } } function showError(message) { document.getElementById('result').innerHTML = 'Error: ' + message; } </script> </head> <body> <div> <button onclick='getOAuthToken()'>Select a file</button> <p id='result'></p> </div> <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script> </body> </html>
Additional Checks to Ensure It Works
- Enable Required APIs: Go to the Google Cloud Console linked to your script, and make sure both the Google Picker API and Google Drive API are enabled.
- API Key Setup: Double-check that your
DEVELOPER_KEYis valid, and that you've added your script's origin (e.g.,https://script.google.com) to the API key's allowed referrers. - Authorize the Script: When you first run the
showPickerfunction, you'll need to grant the script permission to access your Google Drive—follow the prompts to complete authorization.
内容的提问来源于stack exchange,提问作者PHioNiX

