【问题标题】:Highlight and find position (row & column) of matching cells in two Google Sheets using Apps Script使用 Apps 脚本在两个 Google 表格中突出显示并查找匹配单元格的位置(行和列)
【发布时间】:2021-06-23 22:25:28
【问题描述】:

我有两张表格我正在动态比较匹配值。 列的位置可以在工作表中的任何位置,脚本仍然可以工作。

我想在for loop 的两个工作表中找到匹配单元格的rowcolumn 位置值并突出显示这些单元格。

我能够为 CASES 表中的值使用它,但我无法在 IMPORT 表中找到匹配单元格的行值来在 IMPORT 表中进行背景突出显示。

我对使用条件格式不感兴趣。

谢谢。

工作表演示链接:DEMO SHEET

我的代码:

function compareSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Load all Casen Number columns values into array
  var importValues = getColumnValues("Case Number", "IMPORT");
  var casesValues = getColumnValues("Case Number", "CASES");
  // Convert Casen Number columns values into 1D array
  var newImportValues = importValues.map(function (row) { return row[0]; });
  var newCasesValues = casesValues.map(function (row) { return row[0]; });


  for (var i in newCasesValues) {
    var matchedCase = newImportValues.indexOf(newCasesValues[i])
    if (matchedCase !== -1) {
      
      // If found then... 
      
      // HIGHLIGHT MATCHING CELL IN CASES TAB
      var tempCasesColIndex = getColumnIndex("Case Number", "CASES")
      ss.getSheetByName("CASES").getRange(+i + 2, tempCasesColIndex).setBackground("orange")
      Logger.log("Case match found in CASES sheet at row: " + (+i + 2) + " & column: " + tempCasesColIndex)

      // HIGHLIGHT MATCHING CELL IN IMPORT TAB
      var tempImportColIndex = getColumnIndex("Case Number", "IMPORT")
      Logger.log("Case match found in IMPORT sheet at row: " + "???" + " & column: " + tempImportColIndex)
      //ss.getSheetByName("IMPORT").getRange(???, tempImportColIndex).setBackground("green")

    } else {
      
      // If NOT found then...
      // Clear the cell formatting
      var tempCasesColIndex = getColumnIndex("Case Number", "CASES")
      ss.getSheetByName("CASES").getRange(+i + 2, tempCasesColIndex).setBackground(null)
    }
  }

}

// Load Case Number column values
function getColumnValues(label, sheetName) {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);

  // Get column number for Case Number
  var colIndex = getColumnIndex(label, sheetName);

  // Get number of rows in Case Number
  var numRows = ss.getLastRow() - 1;

  // Load Case Number values into array
  var colValues = ss.getRange(2, colIndex, numRows, 1).getValues();

  return colValues;

}

// Get column name index value
function getColumnIndex(label, sheetName) {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);

  // Find last column
  var lc = ss.getLastColumn();

  // Load headers into array
  var lookupRangeValues = ss.getRange(1, 1, 1, lc).getValues()[0];

  // Search for label and return the column number
  var index = lookupRangeValues.indexOf(label) + 1;

  return index;

}

【问题讨论】:

    标签: javascript arrays google-apps-script google-sheets


    【解决方案1】:

    在找到一个解释方法findIndex 的 YT 视频后,我设法解决了这个问题。 现在我可以从两张表中返回匹配值的位置。

    如果有人感兴趣: Link to the video - Array find( ) and findIndex( ) Methods explained

      // Find matching cell row number in the IMPORT sheet
      let tempImportRowIndex = newImportValues.findIndex(item => {
        if (newCasesValues[i] === item) return true;
      });
      tempImportRowIndex = tempImportRowIndex + 2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多