'Google Sheets Script: That Searches For Previous Match in Memo and If Found Automaticly Fills In A Category

I am trying to edit a google sheets budgeting template. I need a script that looks at previous memos that have been assigned a category and will match newly entered memos with a category if it has already been matched above.

Memos consist of multiple words and a match should only happen if the exact words are present.

What Spreadsheet Looks Like

Spreadsheet Link

I don't know if this is relevant but the template consists of multiple sheets.

I found someone else's code (Source) trying to do what I do but I cant get it to work. This Is what they did...

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Aspire Budgeting')
      .addItem('Auto Fill Category. Click on first empty Category in Transaction first', 'autoFillCategory')
      .addToUi();
}

function autoFillCategory() {
  // to use this script import new Transactions then click on the first empty category in Transactions.  
  var sheet = SpreadsheetApp.getActiveSheet();
  // startRow is off set by 1 in getDataRange
  var startRow = sheet.getActiveCell().getRow() - 1;
  // categoryColumn is off set by 1 in getDataRange
  var categoryColumn = sheet.getActiveCell().getColumn() -1;
  var memoColumn = categoryColumn + 2
  var data = sheet.getDataRange().getValues();
  
    
  // Check to make sure the current cell is set to Transactions -> Categories
  if (sheet.getName() == "Transactions" && data[6][categoryColumn] == "Category" && startRow > 8) {
    for (var currRow = startRow; currRow < data.length; currRow++) {
      // memoValue to search for
      var memoValue = data[currRow][memoColumn];
      
      //  SpreadsheetApp.getUi().alert(currRow);
      var previousCategory = "";
      //Search for the previous instance of memoValue.
      for (var i = 0; i < currRow; i++) {
        var row = data[i];
        if (row[memoColumn] == memoValue) {
          previousCategory=row[categoryColumn];
          sheet.getRange(currRow + 1,categoryColumn + 1).setValue(previousCategory);
          break;
        }
      }
    }   
  } else { 
    SpreadsheetApp.getUi().alert("Before running this script import new transactions then click on the first empty category in transactions you want to search for.");
    return;
  }
}

I dont really have much coding experience so I don't really where Im going wrong



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source