如何移除嵌入Google扩展的Google Sheet边框?
Got it, let's tackle that annoying Google Sheet UI in your Chrome extension! The built-in options might be broken, but we've got two solid ways to fix this—one quick CSS hack, and a more reliable API approach that gives you full control over what's displayed.
方案1:CSS快速隐藏顶部UI(简单快捷)
This method works by clipping the iframe to hide the unwanted header and tab bar, no JavaScript required:
- Wrap your iframe in a container with
overflow:hiddento crop any content that spills outside - Adjust the iframe's position to push the top UI elements out of view
Here's the modified HTML:
<html> <link rel="stylesheet" href="extension.css"> <!-- Container to clip overflow --> <div style="width: 250px; height: 250px; overflow: hidden; position: relative;"> <!-- Shift iframe up to hide top UI, adjust height to compensate --> <iframe src="https://docs.google.com/spreadsheets/d/e/2PACX-1vQhhQM1ag9M_exkDEV8omFWuI5-4tRizOZ5pl0ZzxpAEJ3rHfWzWilEzjgV4jwpyEj2aaTUkfQx6DRK/pubhtml?gid=1478993491&single=true&widget=true&headers=false" style="position: absolute; top: -48px; left: 0; width: 250px; height: 300px; border: none;" ></iframe> </div> </html>
You might need to tweak the top value (try -45px to -50px) to perfectly hide the header bar—Google’s embedding layout can shift slightly.
方案2:Google Sheets API(更可靠,完全自定义)
If you want long-term stability (since Google could change their embed UI anytime), use the Sheets API to pull just the TEAM and SCORE data and render your own table. This gives you full control over styling and content.
Step 1: Set up the API
- Go to the Google Cloud Console, create a project, and enable the Google Sheets API
- Generate an API key, and restrict it to your extension for security
- Make sure your Google Sheet is set to "Anyone with the link can view"
Step 2: Add the code to your extension
Replace the iframe with this HTML/JS combo to fetch and render your table:
<html> <link rel="stylesheet" href="extension.css"> <div id="scoreTable"></div> <script> // Replace these with your own API key and sheet details const API_KEY = 'YOUR_API_KEY_HERE'; const SHEET_ID = '2PACX-1vQhhQM1ag9M_exkDEV8omFWuI5-4tRizOZ5pl0ZzxpAEJ3rHfWzWilEzjgV4jwpyEj2aaTUkfQx6DRK'; const RANGE = 'Sheet2!A:B'; // Update if TEAM/SCORE are in different columns // Fetch data from the Sheets API fetch(`https://sheets.googleapis.com/v4/spreadsheets/${SHEET_ID}/values/${RANGE}?key=${API_KEY}`) .then(res => res.json()) .then(data => { const rows = data.values; if (!rows || rows.length === 0) { document.getElementById('scoreTable').innerHTML = '<p>No score data found</p>'; return; } // Build custom table HTML let tableHtml = '<table style="border-collapse: collapse; width: 100%;">'; // Add header row tableHtml += `<tr><th style="border: 1px solid #ddd; padding: 8px;">${rows[0][0]}</th><th style="border: 1px solid #ddd; padding: 8px;">${rows[0][1]}</th></tr>`; // Add data rows for (let i = 1; i < rows.length; i++) { const [team, score] = rows[i]; tableHtml += `<tr><td style="border: 1px solid #ddd; padding: 8px;">${team || ''}</td><td style="border: 1px solid #ddd; padding: 8px;">${score || ''}</td></tr>`; } tableHtml += '</table>'; // Render the table document.getElementById('scoreTable').innerHTML = tableHtml; }) .catch(err => { console.error('Failed to load sheet data:', err); document.getElementById('scoreTable').innerHTML = '<p>Error loading scores</p>'; }); </script> </html>
Extension Manifest Note
Don’t forget to add the Sheets API domain to your manifest.json permissions:
"permissions": [ "https://sheets.googleapis.com/*" ]
内容的提问来源于stack exchange,提问作者reisnern21

