请求开发Google Drive私有文件夹密码验证触发脚本
Password-Protected Access for Your Google Drive "Private" Folder
Hey there! Let's work through this. First, a quick heads-up: Google Drive doesn't have a built-in trigger that fires when someone opens a specific folder, so we'll need a workaround to add password protection for your "Private" folder. Below is a practical, beginner-friendly solution that uses Google Apps Script to create a gated access entry.
Step 1: Prep Your Folder & Credentials
- Make sure your "Private" folder exists in Google Drive. Grab its folder ID from the URL (it's the long string after
drive/folders/in the address bar). - Pick a secure password you want to use for verification (e.g.,
MySecurePass_2024).
Step 2: Set Up the Apps Script Project
- Open Google Apps Script and create a blank project.
- Replace the default
Code.gscode with this (don't forget to update the config section!):
// 🛠️ Configuration - Update these values to match your setup! const SETTINGS = { VERIFICATION_PASSWORD: "MySecurePass_2024", PRIVATE_FOLDER_ID: "your-folder-id-here", PRIVATE_FOLDER_URL: "https://drive.google.com/drive/folders/your-folder-id-here" }; // 🎯 Creates a custom menu in Google Sheets (our access entry point) function onOpen() { const ui = SpreadsheetApp.getUi(); ui.createMenu('Private Folder Access') .addItem('Unlock Private Folder', 'showPasswordPrompt') .addToUi(); } // 🔒 Shows password prompt and handles verification function showPasswordPrompt() { const ui = SpreadsheetApp.getUi(); const userResponse = ui.prompt('Enter Access Password', ui.ButtonSet.OK_CANCEL); // Handle if user cancels if (userResponse.getSelectedButton() === ui.Button.CANCEL) { ui.alert('Access cancelled.'); return; } // Check password if (userResponse.getResponseText() === SETTINGS.VERIFICATION_PASSWORD) { ui.alert('Password verified! Opening your private folder...'); // Open the folder in a new tab const redirectHtml = HtmlService.createHtmlOutput(` <script> window.open('${SETTINGS.PRIVATE_FOLDER_URL}', '_blank'); google.script.host.close(); </script> `).setWidth(150).setHeight(100); ui.showModalDialog(redirectHtml, 'Loading...'); } else { ui.alert('Incorrect password. Access denied.'); } } // 🚨 Optional: Monitor folder access attempts (less precise) function onDriveChange(e) { const changedItem = DriveApp.getFileById(e.source.getId()); if (changedItem.getName() === 'Private' && e.changeType === 'EDIT') { // Send yourself an alert if someone tries to modify the folder MailApp.sendEmail('your-email@example.com', 'Private Folder Access Attempt', 'Someone tried to access or edit your Private folder.'); } }
Step 3: Configure & Test
- Update the
SETTINGSsection: Replace the placeholder password, folder ID, and URL with your actual values. - Authorize the script: Run the
onOpenfunction once. You'll need to grant permissions (Google will warn you it's an "unverified app"—this is safe since you wrote it! Click "Advanced" > "Go to [Project Name]" to proceed). - Test the workflow:
- Create a blank Google Sheet, open its script editor (Tools > Script editor), and paste the updated code.
- Refresh the sheet—you'll see a new
Private Folder Accessmenu at the top. - Click "Unlock Private Folder", enter your password, and you'll be redirected to the Private folder if correct.
Step 4: Optional Hardening (For Extra Security)
- Restrict folder permissions: Set your "Private" folder to be accessible only to you. After password verification, the script can temporarily grant the user access (use
DriveApp.getFolderById(SETTINGS.PRIVATE_FOLDER_ID).addViewer(userEmail)and set a timer to remove access later). - Hide the password: Store your password in a hidden cell of a separate Google Sheet instead of hardcoding it in the script. Use
SpreadsheetApp.openById('sheet-id').getRange('A1').getValue()to fetch it. - Use a Web App: Deploy the script as a Web App so users can access it directly via a URL, no spreadsheet required.
Important Notes
- This solution relies on users using the custom menu/Web App to access the folder. It can't block direct access via the folder URL unless you lock the folder down completely (only you have access) and use the temporary permission trick above.
- Make sure to keep your script's permissions up to date, and avoid sharing the script code with untrusted users.
内容的提问来源于stack exchange,提问作者Manel Aguayo
相关产品推荐
相关产品推荐

