forEach array getting error TypeError: Cannot read property 'forEach' of undefined

forEach array getting error TypeError: Cannot read property 'forEach' of undefined

我正在尝试清理一些 sheets,我单独让它工作,取消注释并更改 sheet 名称

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: Cannot read property 'forEach' of undefined doCleanSheets @ - 10x_to_11x.gs:87

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

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

当我看到你的脚本时,不幸的是,SpreadsheetApp.getActive() 没有 sheet 的 属性。这样,就会出现这样的错误。当你想在forEach中使用sheet的sheet名字时,下面的修改如何?

修改后的脚本:

请修改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);
}

参考文献:

这样试试:

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)));
};