代码用途:在注释中记录每个已编辑单元格的编辑日期,然后在其他工作表(包括当前工作表)中搜索该单元格值的重复项,然后添加工作表名称和重复项(如果有)的行位置。
问题:在编辑单个单元格或粘贴最多8或9个单元格的范围时,代码可以正常工作,但是更大的范围,代码将在第9或第10单元格的日期记录步骤停止。
我认为在宏启动后选择另一个单元格时,代码可能会感到困惑,因此,我确保没有任何事情试图捕获活动单元格。
我认为这可能是因为它过于占用资源,并且已尝试优化代码(我知道它非常笨拙且效率低下,如果您有任何关于如何优化它的技巧,请告诉我)。
我尝试摆弄数字并搜索9、10、15之类的数字,以查看是否在代码中不知不觉地放置了数字限制器。
function onEdit(e){
var keyword1 = "未確定"
var keyword2 = "確定用語集"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var activeSheetName = activeSheet.getName();
//check to see if this is a sheet we want the macro to be running on
if ((activeSheet.getName().indexOf(keyword1)>-1 || activeSheet.getName().indexOf(keyword2)>-1) && e.range.columnStart >= 6 && e.range.rowStart >= 3 ){
//--------------------------------------------------------------------------------------------------------------------
//make list of searchable sheets (for dupe searching later)
var allSheets = ss.getSheets();
var confirmSheet = ss.getSheetByName(keyword2);
var allSheetsNameArray = allSheets.map(function(sheet){return [sheet.getName()];});
var targetSheetArray = [confirmSheet.getName()];
//build target sheet list
for(var a = 0; a < allSheetsNameArray.length; a++){
if(allSheetsNameArray[a][0].indexOf(keyword1) > -1){
targetSheetArray.push(allSheetsNameArray[a][0])
}
};
//--------------------------------------------------------------------------------------------------------------------
// Then start looking at each cell in the edited range
var range = e.range;
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
//When looking, get the currently counted cell's coordinates and value
var currentCell = range.getCell(i,j)
var currentValue = currentCell.getValue();
//Before starting, reset cell color
//currentCell.setBackground(null)
//if the cell is blank, DO NOTHING
if (currentValue != ""){
var message = currentCell.getNote()
//Set a note on it with the contents being: a copy of the previous note (returns blank if none), the date formatted year/month/day/time, followed by a separator and the currently entered value.
message += (
Utilities.formatDate(new Date(), "GMT+9","yy/MM/dd HH:mm") + " | " +
currentValue +
String.fromCharCode(10)
);
//--------------------Problem area?-----------------------------
//check for duplicates
//loop through every name in the list of sheet we want to search (list made at beginning of code)
for (var b = 0; b <targetSheetArray.length; b++){
//targetSheet is the sheet we will be looking for duplicates in, sourceSheet is the sheet we are on and contains the word we want to search
var targetSheet = ss.getSheetByName(targetSheetArray[b]);
var sourceSheet = activeSheet;
var sourceCell = currentCell;
//store all values in the target sheet into an array
var values = targetSheet.getDataRange().getValues();
var searchString = sourceCell.getValue();
var column = sourceCell.getColumn();
//loop through every item in the value array
for(var k=0; k < values.length; k++) {
//find out if the value we're searching equals a value in the array. Value must not be in the same position as our current source cell.
if(values[k][column-1] == searchString && k+1 != sourceCell.getRow()) {
//If we've found a duplicate we make a note of what sheet and what row it's on.
message += ("└ 重複:" + targetSheetArray[b]+ (k+1) + "行目"+ String.fromCharCode(10));
};
};
};
sourceCell.setNote(message);
//--------------------------------------------------------------------
}
}
}
}
};
该代码应该能够处理粘贴到工作表中的任意数量的单元格/值。
更新:我已经清理了一些代码,经过一些试验,我发现如果删除重复检查部分,代码将在整个范围内继续运行。也许这就是问题所在?