读取 xlsx 文件并搜索特定值

Read an xlsx file and search for a specific value

我需要在 A 列的 xlsx 中搜索一个值,return 其他列的值 [B-G]

我有一个读取 xlsx 的函数:

userData.js

function personData (){

    let XLSX = require('xlsx')
    let workbook = XLSX.readFile(`${__dirname}/\person.xlsx`);
    let sheet_name_list = workbook.SheetNames;
    let xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
    return xlData
}
module.exports = personData;

和return数据如下

  {
    id: 0,
    name: 'Bob', //I need to search for this attribute and return all others
    full_name: 'Robert Shawn',
    number: 123456789,
    team: 'IT',
    profile: 'ALL',
    file: 5672,
    email: 'robsh@gmail.com'
  },
  {
    id: 1,
    name: 'Jean',
    full_name: 'Mary Jean',
    number: 987654321,
    team: 'HR',
    profile: 'Sales',
    file: 1285,
    email: 'mjean@gmail.com'
  }
]

现在我需要在该结果和 return 其他信息中搜索一个值 类似于:

user.js

const usersData = require ('./userData')
let valueToSearch = 'Bob' //search for that value inside the name attribute

let personData = usersData(valueToSearch)
console.log(personData); //return Bob's data

谢谢!

鉴于 userData 中已经存在的数据,您可以使用 Array.find() 查找具有匹配名称的对象 (row)。

userData.find(o => o.name === valueToSearch);

如果找到这样的对象,您可以通过Object.keys()提取其属性名称,过滤掉不需要的属性并提取相关值,如下所示。

Object.keys(row)
  .filter(k => k !== 'id' && k !== 'name')
  .map(k => row[k])
  .join(', ');

请查看下面的可运行代码片段。

const userData = [{
    id: 0,
    name: 'Bob', //I need to search for this attribute and return all others
    full_name: 'Robert Shawn',
    number: 123456789,
    team: 'IT',
    profile: 'ALL',
    file: 5672,
    email: 'robsh@gmail.com'
  },
  {
    id: 1,
    name: 'Jean',
    full_name: 'Mary Jean',
    number: 987654321,
    team: 'HR',
    profile: 'Sales',
    file: 1285,
    email: 'mjean@gmail.com'
  }
];
const valueToSearch = 'Bob';

const row = userData.find(o => o.name === valueToSearch);
if (row) {
  const values = Object.keys(row)
  .filter(k => k !== 'id' && k !== 'name')
  .map(k => row[k])
  .join(', ');
  console.log('found: ' + values)
} else {
  console.log('not found');
}

使用 search 函数为来自 json 和特定键的搜索值创建了一个自定义 searchUsersData 函数。

/**
 * @Info: This function will return searched object
 * @Arg1: data => your user json data
 * @Arg2: searchValue => value that you want to search
 * @Arg3: searchKey => key name that you want to search value inside this key
*/
function searchUsersData(data, searchValue, searchKey){
  let returnData = [];
  data.forEach((obj, objKey) => {
      if( obj[searchKey].toString().search(searchValue.toString()) >= 0 )
         returnData.push(obj);
  });
  return returnData;
}

const usersData = [{
    id: 0,
    name: 'Bob', //I need to search for this attribute and return all others
    full_name: 'Robert Shawn',
    number: 123456789,
    team: 'IT',
    profile: 'ALL',
    file: 5672,
    email: 'robsh@gmail.com'
  },{
    id: 1,
    name: 'Jean',
    full_name: 'Mary Jean',
    number: 987654321,
    team: 'HR',
    profile: 'Sales',
    file: 1285,
    email: 'mjean@gmail.com'
  },{
    id: 0,
    name: 'Test Bob', //I need to search for this attribute and return all others
    full_name: 'Robert Shawn',
    number: 123456789,
    team: 'IT',
    profile: 'ALL',
    file: 5672,
    email: 'robsh@gmail.com'
}];

let valueToSearch = 'Bob'; //search for that value inside the name attribute
// specify a key name that you want to search inside, i have passed 'name' key
// SEARCH NAME
let personData = searchUsersData(usersData, valueToSearch, 'name');
console.log('name search: ', personData); //return Bob's data

// SEARCH NUMBER
valueToSearch = 123456789;
personData = searchUsersData(usersData, valueToSearch, 'number');
console.log('number search: ', personData); //return Bob's data

我添加了 3 个对象用于测试目的,搜索 Bob,这将 return 2 个对象,因为 2 个对象的名称键中有 Bob 词。

希望对您有所帮助。