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

如何在Google表格Apps Script宏中设置时间条件触发CSV导入

Google Apps Script 定时CSV导入脚本修复方案

原有脚本核心错误

  • 语法错误:时间变量time01/timeT是普通变量,代码里错误加()按函数调用;if判断分支用逗号分隔不符合JS语法,无法正确执行二值判断;粘贴类型枚举CopyPastType拼写错误且缺少SpreadsheetApp前缀,运行会直接报错
  • 逻辑错误:Utilities.formatDate()返回字符串类型,直接调用.valueOf()无法拿到可比较的时间戳,时间判断完全失效;缺少工作日判断,周末会无意义执行;未预留触发器延迟容错窗口,容易错过数据拉取时机
  • 流程缺陷:复制粘贴值前未调用刷新方法,IMPORTDATA公式还没计算出结果就会被静态值覆盖,拿到空数据

修复后可用代码

function Loaddata() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const s1 = ss.getSheetByName('import');
  const IST_TZ = "Asia/Kolkata"; // 固定IST时区ID,避免缩写歧义

  // 1. 基础变量获取
  const range_1 = s1.getRange('B6');
  const cell_1 = s1.getRange('E2'); // 状态标识单元格
  const cell_2 = s1.getRange('I2'); // 触发时间阈值单元格
  const rownum = range_1.getRowIndex();

  // 2. 日期判断(仅当日未导入时执行插行操作)
  const currentDate = new Date();
  const date02 = Utilities.formatDate(currentDate, IST_TZ, "yyyy-MM-dd");
  let date01 = range_1.getValue();
  date01 = Utilities.formatDate(date01, IST_TZ, "yyyy-MM-dd");

  // 3. 工作日判断:周六(6)、周日(0)直接退出
  const weekDay = currentDate.getDay();
  if (weekDay === 0 || weekDay === 6) return;

  // 4. 时间判断:解析阈值时间,设置E2状态
  const thresholdTime = cell_2.getValue();
  // 把阈值时间转为当日IST的时间戳
  const thresholdTimestamp = new Date(
    Utilities.formatDate(currentDate, IST_TZ, "yyyy-MM-dd") + " " +
    Utilities.formatDate(thresholdTime, IST_TZ, "HH:mm:ss")
  ).getTime();
  const currentTimestamp = currentDate.getTime();
  // 留5分钟容错窗口,避免触发器延迟导致判断失效
  if (currentTimestamp < thresholdTimestamp - 5*60*1000) {
    cell_1.setValue('Previous');
  } else {
    cell_1.setValue('Latest');
  }

  // 5. 当日数据未导入时,执行插行、复制、拉取操作
  if (date01 < date02) {
    s1.insertRowsBefore(rownum, 15);
    // 预定义范围
    const range_2 = s1.getRange('A5:B5');
    const range_3 = s1.getRange('C5');
    const range_4 = s1.getRange('K5');
    const range_5 = s1.getRange('D7:D20');
    const range_6 = s1.getRange('D7:I20');
    const range_7 = s1.getRange('B6:I21');
    const rangeTarget_2 = s1.getRange('A6:B21');
    const rangeTarget_3 = s1.getRange('C6');
    const rangeTarget_4 = s1.getRange('K6');
    const rangeTarget_7 = s1.getRange('B6:I21');

    // 复制格式与公式
    range_2.copyTo(rangeTarget_2);
    range_3.copyTo(rangeTarget_3);
    range_4.copyTo(rangeTarget_4);
    range_5.setHorizontalAlignment('left');
    range_6.setFontWeight(null).setBackground('BACKGROUND');

    // 等待IMPORTDATA公式计算完成
    SpreadsheetApp.flush();
    // 等待3秒确保数据拉取完成,网络慢可适当调大
    Utilities.sleep(3000);

    // 粘贴为静态值
    range_7.copyTo(rangeTarget_7, SpreadsheetApp.CopyPasteType.PASTE_VALUES);
  }
}

配置说明

  • I2单元格直接填写18:00:00即可,无需特殊格式
  • 打开脚本编辑器左侧「触发器」菜单新建定时任务:触发源选时间驱动,类型选每周,勾选周一至周五,执行时段选18:00-19:00,时区选择Asia/Kolkata(IST),无需在脚本内做高频轮询判断,可节省Google服务配额
  • 如果CSV拉取经常出现空值,可把代码里Utilities.sleep(3000)的数值调大到5000(等待5秒)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 16:36:43