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

求助:如何让Google Apps Script循环输出所有IF条件错误行

问题:Google Apps Script无法循环列出所有标记的错误行

我一直关注相关内容,刚接触Google Apps Script。写了个脚本检查大型表格内容是否填写完整,目前脚本能运行,但每种错误只能显示单个结果,希望能循环列出所有被IF语句标记的行(已简化代码,删除大量IF语句)。

初始代码

function errorTest() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Ad Status");
  var values = sheet.getDataRange().getValues();
    var missingFBorder = "";
    var stillFB = "";

    for(var i = 0; i < values.length; i++) {
      // 检查第0列为启用状态但第15列为空,输出第4、5、1列:城市、州(类型)
        if((values[i][0] === "Enabled"||values[i][0] === "Pending Enable"||values[i][0] === "Pending Month-end") && values[i][15] == "") {
            var missingFBorder = "FB order # missing: ";
            missingFBorder += values[i][4] + ", " + values[i][5] + " (" + values[i][1] + ")\n"
        }
        
      // 检查第0列为禁用状态但第14列不为false,输出第4、5、1列:城市、州(类型)
        if((values[i][0] === "Disabled"||values[i][0] === "Pending Disable") && values[i][14] != false) {
            var stillFB = "FB ad set still running:";
            stillFB += values[i][4] + ", " + values[i][5] + " (" + values[i][1] + ")\n"
        }
    }
    errorTxt = missingFBorder + stillFB;
console.log(errorTxt);
  }

这段代码返回单个错误时表现正常,但无法按预期返回多个错误。


更新:实现功能但代码不够优雅

已解决变量被重复覆盖的问题(感谢Cooper指出),现在会在添加标题前检查变量是否为空,为空则跳过。

function errorTest() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("YRC Ad Status");
  var values = sheet.getDataRange().getValues();
    var missingFBorder = "";
    var stillFB = "";

    for(var i = 0; i < values.length; i++) {
      // 检查第0列为启用状态但第15列为空,输出第4、5、1列:城市、州(类型)
        if((values[i][0] === "Enabled"||values[i][0] === "Pending Enable"||values[i][0] === "Pending Month-end") && values[i][15] == "") {
            missingFBorder += values[i][4] + ", " + values[i][5] + " (" + values[i][1] + ")\n"
        }
        
      // 检查第0列为禁用状态但第14列不为false,输出第4、5、1列:城市、州(类型)
        if((values[i][0] === "Disabled"||values[i][0] === "Pending Disable") && values[i][14] != false) {
            var stillFB = "FB ad set still running:";
            stillFB += values[i][4] + ", " + values[i][5] + " (" + values[i][1] + ")\n"
        }
    }
    if(missingFBorder != "") {
    var missingFBorder = "FB order # missing: " + missingFBorder;
    }
    if(stillFB != "") {
    var stillFB = "FB ad set still running:"; + missingFBorder;
    }
    
    errorTxt = missingFBorder + stillFB;
console.log(errorTxt);
  }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 13:18:21