如何使用JavaScript和ExcelJS检查不同工作表中的单元格值?

时间:2018-11-29 17:01:23

标签: javascript excel exceljs

我有一个包含两张纸的电子表格。 Sheet 1包含3列,其中包含100行数据(字符串)。 Sheet 2用作正确检查Sheet 1组合的母版,以确保单元格中的值正确。 Sheet 2也有3列。

我想做的是逐行遍历Sheet 1,并检查行中每个单元格的值是否与任意位置匹配Sheet 2中的每一行

如果检查失败,则应停止对该行的进一步检查,并开始下一个要检查的行。 Sheet 1中无法匹配的单元格应标记为红色。

下面的代码与我的代码接近,但是标记出的单元格比要求的要多。

// Import the library
var Excel = require('exceljs');
var moment = require('moment');
// Define Excel filename
var ExcelFile = 'so.xlsx';

// Read from the file
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(ExcelFile)
    .then(function() {
        // Use workbook
        var dataSheet = workbook.getWorksheet('Sheet 1');
        var masterSheet = workbook.getWorksheet('Sheet 2');

        dataSheet.eachRow({ includeEmpty: false }, function(row, rowNumber) {

            var dataSheetCell1 = row.getCell('A').value;
            var dataSheetCell2 = row.getCell('B').value;
            var dataSheetCell3 = row.getCell('C').value;

            masterSheet.eachRow({ includeEmpty: false }, function(row, rowNumber) {

                var masterSheetCell1 = row.getCell('A').value;
                var masterSheetCell2 = row.getCell('B').value;
                var masterSheetCell3 = row.getCell('C').value;

                // Iterate over all cells in a row (including empty cells)
                row.eachCell({ includeEmpty: false }, function(cell, colNumber) {
                    if(dataSheetCell1 == masterSheetCell1) {
                        console.log(true)
                    } else {
                        // Stop all further checks for this sheet(n) row and move onto next row
                        // Mark this failed cell as color red
                    }
                });
            });
        });

        return workbook.xlsx.writeFile('new.xlsx');
    });

示例数据如下:

第1页:

| COL A | COL B  | COL C  |
|-------|--------|--------|
| bob   | eleven | blue   |
| bob   | eleven | blue   |
| bob   | eleven | red    |
| bob   | eleven | red    |
| bob   | one    | red    |
| bob   | eight  | red    |
| bob   | eight  | red    |
| bob   | eight  | red    |
| terry | seven  | yellow |
| terry | seven  | yellow |
| terry | seven  | gold   |

表2:

| COL A | COL B  | COL C  |
|-------|--------|--------|
| bob   | eleven | blue   |
| bob   | eleven | red    |
| bob   | eight  | red    |
| terry | seven  | yellow |
| terry | seven  | orange |

根据示例数据,应该有两个错误(B5C11)在Sheet 1的{​​{1}}中标记为红色。例如

enter image description here

这是example PDF进行检查的方式:

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以将每个失败的单元格标记为红色,如下所示:

// Import the library
var Excel = require('exceljs'),
    moment = require('moment'),
    // Define Excel filename
    ExcelFile = 'so.xlsx',
    // Read from the file
    workbook = new Excel.Workbook();

workbook.xlsx.readFile(ExcelFile)
    .then(function()
    {
        // Use workbook
        var dataSheet = workbook.getWorksheet('Sheet 1'),
            masterSheet = workbook.getWorksheet('Sheet 2');

        dataSheet.eachRow({ includeEmpty: false }, function(dataRow, dataRowNumber)
        {
            var dataRowCells =
                {
                    dataCell1: dataRow.getCell('A'),
                    dataCell2: dataRow.getCell('B'),
                    dataCell3: dataRow.getCell('C')
                },
                isdataRowOK = false,
                oneOfBestMasterRowNumber,
                cellNames = ['A','B','C'];

            masterSheet.eachRow({ includeEmpty: false }, function(masterRow, masterRowNumber)
            {
                if(!isdataRowOK)
                {
                    var numberOfGoodCellsInRow = 0;

                    for(var i = 1; i < 4; i++)
                        if(dataRowCells['dataCell' + i].value === masterRow.getCell(cellNames[i-1]).value)
                            numberOfGoodCellsInRow++;

                    if(numberOfGoodCellsInRow == 2)
                        oneOfBestMasterRowNumber = masterRowNumber;

                    if(numberOfGoodCellsInRow == 3)
                        isdataRowOK = true
                }
            });


            if(!isdataRowOK)
            {
                var masterRowForCheck = masterSheet.getRow(oneOfBestMasterRowNumber);

                for(var i = 1; i < 4; i++)
                {
                    var dataCell = dataRowCells['dataCell' + i];
                    if(dataCell.value !== masterRowForCheck.getCell(cellNames[i-1]).value)
                    {
                        // Mark this failed cell as color red
                        dataCell.style = Object.create(dataCell.style); // Shallow-clone the style, break references
                        dataCell.fill = {type: 'pattern', pattern:'solid', fgColor:{argb:'FA8072'}}; // Set background
                    }
                }

            }
        });

        return workbook.xlsx.writeFile('new.xlsx');
    });