繁体   English   中英

forEach 数组出现错误类型错误:无法读取未定义的属性“forEach”

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

我试图清理一些床单,我单独让它工作,取消注释并更改床单名称

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

我收到错误

类型错误:无法读取未定义的 doCleanSheets 的属性“forEach”@ - 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)));
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM