You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

谷歌表格importxml等函数失效,求Google Apps Script取数方案(UrlFetch受限)

解决方案:Chrome扩展 + Google Apps Script 实现数据提取与写入

由于Google Apps Script无法直接读取Chrome标签页内容,需通过Chrome扩展抓取页面数据,再借助GAS Web App将数据传入谷歌表格,以此规避UrlFetch禁用及网页访问限制问题。

步骤1:创建Chrome扩展

创建以下3个文件,放在同一文件夹内:

1.1 manifest.json(扩展配置)

{
  "manifest_version": 3,
  "name": "页面数据提取工具",
  "version": "1.0",
  "permissions": ["activeTab", "scripting"],
  "host_permissions": ["https://script.google.com/macros/s/*"],
  "action": {
    "default_popup": "popup.html"
  }
}

1.2 popup.html(扩展弹窗)

<!DOCTYPE html>
<html>
  <body style="width: 200px; padding: 15px;">
    <button id="extractBtn">提取并写入表格</button>
    <script src="popup.js"></script>
  </body>
</html>

1.3 popup.js(弹窗逻辑)

document.getElementById('extractBtn').addEventListener('click', async () => {
  const [tab] = await chrome.tabs.query({active: true, currentWindow: true});
  
  const result = await chrome.scripting.executeScript({
    target: {tabId: tab.id},
    func: extractPageData
  });

  const pageData = result[0].result;
  
  // 替换为你的GAS Web App URL
  const response = await fetch('https://script.google.com/macros/s/你的Web App ID/exec', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(pageData)
  });

  const msg = await response.text();
  alert(msg);
});

// 内容脚本:提取页面文本和表格数据
function extractPageData() {
  const pageText = document.body.innerText.trim();
  
  const tables = Array.from(document.querySelectorAll('table')).map(table => {
    return Array.from(table.querySelectorAll('tr')).map(row => {
      return Array.from(row.querySelectorAll('td, th')).map(cell => cell.innerText.trim());
    });
  });

  return {pageText, tables};
}

步骤2:创建Google Apps Script

打开目标谷歌表格,点击「扩展程序」→「Apps Script」,替换默认代码为:

function doPost(e) {
  try {
    const data = JSON.parse(e.postData.contents);
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    
    // 清空现有内容(可选)
    sheet.clear();
    
    // 写入页面文本到Z列
    sheet.getRange('Z1').setValue('页面文本:');
    sheet.getRange('Z2').setValue(data.pageText);
    
    // 写入表格数据,表格间空2行
    let startRow = 1;
    data.tables.forEach((table) => {
      if (table.length === 0) return;
      const numRows = table.length;
      const numCols = table[0].length;
      sheet.getRange(startRow, 1, numRows, numCols).setValues(table);
      startRow += numRows + 2;
    });
    
    return ContentService.createTextOutput('数据写入成功').setMimeType(ContentService.MimeType.TEXT);
  } catch (error) {
    return ContentService.createTextOutput(`写入失败:${error.message}`).setMimeType(ContentService.MimeType.TEXT);
  }
}

步骤3:部署GAS为Web App

  1. 在GAS编辑器中,点击「部署」→「新建部署」
  2. 选择「Web应用」类型,配置:
    • 执行账号:选择你自己的账号
    • 访问权限:选择「任何人,甚至匿名」(若需限制可调整,确保Chrome扩展能访问)
  3. 部署后复制生成的Web App URL,替换到Chrome扩展popup.js中的fetch地址

步骤4:加载Chrome扩展

  1. 打开Chrome,输入chrome://extensions/
  2. 开启「开发者模式」
  3. 点击「加载已解压的扩展程序」,选择存放扩展文件的文件夹

使用方法

  1. 打开目标HTML页面
  2. 点击Chrome工具栏的扩展图标,点击「提取并写入表格」按钮
  3. 等待弹窗提示成功,返回谷歌表格即可查看数据

内容的提问来源于stack exchange,提问作者MazaM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:05:31