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

Google Apps Script中For/If函数与宏配合失效问题求助

问题修复方案

核心问题分析

  1. getDados 函数仅打印日志,未返回任何符合条件的任务数据,导致 chamaMensagem 无法获取有效信息。
  2. chamaMensagem 中硬编码数组索引,无法遍历所有状态为 Vencido 的行。
  3. 未对多行符合条件的数据进行批量处理。

修复后的完整代码

// 封装Slack API请求函数(保留原逻辑,优化冗余代码)
function requestSlack(method, endpoint, payload) {
    const base_url = "https://slack.com/api/";
    const headers = {
        'Authorization': "Bearer MYTOKEN", // 替换为你的Slack Bot Token
        'Content-Type': 'application/json'
    };
    const options = {
        headers: headers,
        method: method,
        payload: method === "POST" ? JSON.stringify(payload) : payload
    };
    const response = UrlFetchApp.fetch(base_url + endpoint, options).getContentText();
    const json = JSON.parse(response);
    return {
        response_code: json.ok,
        response_data: json
    };
}

// 获取所有状态为"Vencido"的任务数据
function getDados() {
    const planilha = SpreadsheetApp.getActiveSpreadsheet();
    const guiadados = planilha.getSheetByName("MYSPREADSHEET"); // 替换为你的工作表名称
    const linhainicial = 12;
    const colunainicial = 3;
    const totalcolunas = 6;
    const totalLinhas = guiadados.getLastRow() - linhainicial + 1; // 修正行数计算逻辑

    // 获取指定范围的所有数据
    const dados = guiadados.getRange(linhainicial, colunainicial, totalLinhas, totalcolunas).getValues();
    const colunastatus = 5; // 状态列索引(数组从0开始计数)
    const tarefasVencidas = [];

    // 遍历行数据,收集过期任务
    for (let i = 0; i < dados.length; i++) {
        const linha = dados[i];
        if (linha[colunastatus] === "Vencido") {
            tarefasVencidas.push({
                titulo: linha[0], // 对应表格第3列(起始列)的任务标题
                proprietario: linha[2], // 对应表格第5列的负责人
                deadline: linha[1], // 对应表格第4列的截止日期
                descricao: linha[3], // 对应表格第6列的任务描述
                status: linha[colunastatus]
            });
        }
    }
    return tarefasVencidas;
}

// 遍历所有过期任务,发送Slack通知
function chamaMensagem() {
    const tarefasVencidas = getDados();
    if (tarefasVencidas.length === 0) {
        Logger.log("当前没有过期任务需要通知");
        return;
    }

    const channel = "MYCHANNEL"; // 替换为目标Slack频道ID或名称
    // 逐个发送消息
    tarefasVencidas.forEach(tarefa => {
        const payload = {
            channel: channel,
            text: `*Titulo da tarefa:* ${tarefa.titulo}` +
                  `\n*Proprietario:* ${tarefa.proprietario}` +
                  `\n*Deadline:* ${tarefa.deadline}` +
                  `\n*Descrição:* ${tarefa.descricao}` +
                  `\n*Status:* ${tarefa.status}`
        };
        const response = requestSlack("POST", "chat.postMessage", payload);
        Logger.log(`消息发送状态:${response.response_code}`);
    });
}

关键修复点说明

  • getDados 优化:
    • 修正行数计算逻辑,确保获取完整的数据范围。
    • 新增数组收集所有过期任务的结构化数据,方便后续批量处理。
    • 返回收集到的任务数组,供发送函数调用。
  • chamaMensagem 优化:
    • 遍历所有过期任务,实现批量发送通知。
    • 增加无过期任务时的日志提示,避免无效请求。
    • 移除冗余的charset参数,Slack API无需该字段。
  • requestSlack 优化:
    • 简化请求URL和payload的处理逻辑,减少冗余代码。

使用注意事项

  1. 替换代码中的 MYTOKEN 为你的Slack Bot Token。
  2. 替换 MYSPREADSHEET 为你的目标工作表名称。
  3. 确认 colunastatus 的索引是否与表格中状态列的位置匹配(数组索引从0开始)。
  4. 根据你的表格列顺序,调整 tarefasVencidas.push 中的列索引,确保数据对应正确。
  5. 替换 MYCHANNEL 为目标Slack频道的ID或名称。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 04:36:13