代码之家  ›  专栏  ›  技术社区  ›  Bruno Carvalho

forEach数组获取错误TypeError:无法读取未定义的属性“forEach”

  •  0
  • Bruno Carvalho  · 技术社区  · 2 年前

    我正在尝试清理一些工作表,我会单独取消注释和更改工作表名称

    function doClean(sheet) 
    {
    // var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Resultado");
    
    var LR = sheet.getLastRow();
    var LC = sheet.getLastColumn();
    
    sheet.getRange(1,1,LR,LC).getDisplayValues()
    sheet.createTextFinder("-").matchEntireCell(true).replaceAllWith("");
    sheet.createTextFinder("0").matchEntireCell(true).replaceAllWith("");
    sheet.createTextFinder("0,00").matchEntireCell(true).replaceAllWith("");
    sheet.createTextFinder("0,0000").matchEntireCell(true).replaceAllWith("");
    };
    

    但当我尝试在数组中分组并使用foreach执行时

    function doCleanSheets() {
     var sheet = ["BLC", "Balanço", "DRE", "Resultado", "FLC", "Fluxo", "DVA", "Valor"];
     SpreadsheetApp.getActive().sheet.forEach(doClean);
    };
    

    我出错了

    TypeError:无法读取未定义的属性“forEach” 文件单@-10x到11x。gs:87

    第87行是 SpreadsheetApp.getActive().sheet.forEach(doClean);

    搜索错误,但结果比我的情况复杂得多,我无法申请

    2 回复  |  直到 2 年前
        1
  •  2
  •   Tanaike    2 年前

    不幸的是,当我看到你的剧本时, SpreadsheetApp.getActive() 没有财产 sheet .这样就出现了这样的错误。当您想使用 床单 在forEach中,下面的修改如何?

    修改脚本:

    请修改 doCleanSheets 如下。

    function doCleanSheets() {
      var sheet = ["BLC", "Balanço", "DRE", "Resultado", "FLC", "Fluxo", "DVA", "Valor"];
      var sheets = SpreadsheetApp.getActive().getSheets().filter(s => sheet.includes(s.getSheetName()));
      sheets.forEach(doClean);
    }
    

    参考资料:

        2
  •  1
  •   Cooper    2 年前

    试着这样做:

    function doClean(sheet) {
      var LR = sheet.getLastRow();
      var LC = sheet.getLastColumn();
      sheet.getRange(1, 1, LR, LC).getDisplayValues()
      sheet.createTextFinder("-").matchEntireCell(true).replaceAllWith("");
      sheet.createTextFinder("0").matchEntireCell(true).replaceAllWith("");
      sheet.createTextFinder("0,00").matchEntireCell(true).replaceAllWith("");
      sheet.createTextFinder("0,0000").matchEntireCell(true).replaceAllWith("");
    };
    function doCleanSheets() {
     var sheet = ["BLC", "Balanço", "DRE", "Resultado", "FLC", "Fluxo", "DVA", "Valor"];
     sheet.forEach(sh => doClean(SpreadsheetApp.getActive().geSheetByName(sh)));
    };