Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- //--------------------------------------------------------------------
- }
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement