Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. function onEdit(e){
  2. var keyword1 = "未確定"
  3. var keyword2 = "確定用語集"
  4.  
  5. var ss = SpreadsheetApp.getActiveSpreadsheet();
  6. var activeSheet = ss.getActiveSheet();
  7. var activeSheetName = activeSheet.getName();
  8.  
  9. //check to see if this is a sheet we want the macro to be running on
  10. if ((activeSheet.getName().indexOf(keyword1)>-1 || activeSheet.getName().indexOf(keyword2)>-1) && e.range.columnStart >= 6 && e.range.rowStart >= 3 ){
  11.  
  12.  
  13. //--------------------------------------------------------------------------------------------------------------------
  14. //make list of searchable sheets (for dupe searching later)
  15.  
  16. var allSheets = ss.getSheets();
  17. var confirmSheet = ss.getSheetByName(keyword2);
  18.  
  19. var allSheetsNameArray = allSheets.map(function(sheet){return [sheet.getName()];});
  20. var targetSheetArray = [confirmSheet.getName()];
  21.  
  22. //build target sheet list
  23. for(var a = 0; a < allSheetsNameArray.length; a++){
  24.  
  25. if(allSheetsNameArray[a][0].indexOf(keyword1) > -1){
  26.  
  27. targetSheetArray.push(allSheetsNameArray[a][0])
  28. }
  29. };
  30.  
  31.  
  32. //--------------------------------------------------------------------------------------------------------------------
  33.  
  34.  
  35. // Then start looking at each cell in the edited range
  36. var range = e.range;
  37. var numRows = range.getNumRows();
  38. var numCols = range.getNumColumns();
  39.  
  40. for (var i = 1; i <= numRows; i++) {
  41. for (var j = 1; j <= numCols; j++) {
  42.  
  43. //When looking, get the currently counted cell's coordinates and value
  44. var currentCell = range.getCell(i,j)
  45. var currentValue = currentCell.getValue();
  46.  
  47. //Before starting, reset cell color
  48. //currentCell.setBackground(null)
  49.  
  50. //if the cell is blank, DO NOTHING
  51. if (currentValue != ""){
  52.  
  53. var message = currentCell.getNote()
  54.  
  55. //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.
  56. message += (
  57. Utilities.formatDate(new Date(), "GMT+9","yy/MM/dd HH:mm") + " | " +
  58. currentValue +
  59. String.fromCharCode(10)
  60. );
  61.  
  62.  
  63. //--------------------Problem area?-----------------------------
  64. //check for duplicates
  65.  
  66. //loop through every name in the list of sheet we want to search (list made at beginning of code)
  67. for (var b = 0; b <targetSheetArray.length; b++){
  68.  
  69. //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
  70. var targetSheet = ss.getSheetByName(targetSheetArray[b]);
  71. var sourceSheet = activeSheet;
  72. var sourceCell = currentCell;
  73.  
  74. //store all values in the target sheet into an array
  75. var values = targetSheet.getDataRange().getValues();
  76. var searchString = sourceCell.getValue();
  77. var column = sourceCell.getColumn();
  78.  
  79. //loop through every item in the value array
  80. for(var k=0; k < values.length; k++) {
  81.  
  82. //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.
  83. if(values[k][column-1] == searchString && k+1 != sourceCell.getRow()) {
  84.  
  85. //If we've found a duplicate we make a note of what sheet and what row it's on.
  86. message += ("└ 重複:" + targetSheetArray[b]+ (k+1) + "行目"+ String.fromCharCode(10));
  87.  
  88.  
  89. };
  90. };
  91. };
  92.  
  93. sourceCell.setNote(message);
  94. //--------------------------------------------------------------------
  95.  
  96. }
  97.  
  98. }
  99.  
  100. }
  101. }
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement