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

求助:修复每周自动复制Dropbox Excel指定区域至Google Sheet的脚本

修复后的Google Apps Script脚本

原脚本的核心问题:

  • 直接用UrlFetchApp.fetch获取Dropbox文件,得到的是HTTP响应对象,不是可读取的电子表格,无法直接访问工作表和单元格
  • sourceValues的赋值逻辑完全错误:未从源文件获取数据就直接调用setValues,且参数是未定义的变量
  • 未指定要复制的E3:F区域,反而错误操作了目标工作表的范围

修复后的完整脚本:

function copyDropboxDataToGoogleSheet() {
  // Dropbox文件的直接下载链接
  const dropboxFileUrl = "https://www.dropbox.com/scl/fi/wnidxbxy34chxh2pe8llu/Situazione-Borbone.xlsx?rlkey=2w45eakq91xpa2bqf0igaykb5&e=4&dl=1";
  // 目标Google Sheet的ID
  const targetSpreadsheetId = "19EVUE8VkpUzDUZsv65vwj6ZSD3MZ4sBkkfOYjOEQKWI";
  // 指定的工作表名称
  const sheetName = "P & C S.P.A.";

  try {
    // 1. 从Dropbox下载文件Blob
    const response = UrlFetchApp.fetch(dropboxFileUrl);
    const fileBlob = response.getBlob();

    // 2. 创建临时文件到Drive(用于读取Excel内容)
    const tempFile = DriveApp.createFile(fileBlob);
    const tempSpreadsheet = SpreadsheetApp.open(tempFile);

    // 3. 获取源数据:指定工作表的E3:F区域,过滤空行
    const sourceSheet = tempSpreadsheet.getSheetByName(sheetName);
    if (!sourceSheet) {
      throw new Error(`源文件中未找到工作表:${sheetName}`);
    }
    const sourceRange = sourceSheet.getRange("E3:F");
    const sourceValues = sourceRange.getValues().filter(row => row.some(cell => cell !== ""));

    // 4. 打开目标Google Sheet并粘贴数据
    const targetSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetId);
    const targetSheet = targetSpreadsheet.getSheetByName(sheetName);
    if (!targetSheet) {
      throw new Error(`目标Sheet中未找到工作表:${sheetName}`);
    }
    const targetStartRow = targetSheet.getLastRow() + 1;
    if (sourceValues.length > 0) {
      targetSheet.getRange(targetStartRow, 5, sourceValues.length, 2).setValues(sourceValues);
      console.log(`成功复制 ${sourceValues.length} 行数据到目标工作表`);
    } else {
      console.log("源区域无有效数据,未执行复制");
    }

    // 5. 删除临时文件
    tempFile.setTrashed(true);
  } catch (error) {
    console.error("脚本执行出错:", error.message);
  }
}

使用说明:

  • 直接运行copyDropboxDataToGoogleSheet函数即可执行数据复制
  • 设置每周自动运行:进入脚本编辑器 → 点击左侧「触发器」图标 → 添加触发器,选择该函数,设置时间驱动为每周一次

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 18:05:15