lyg
2024-08-01 ce8cb9c851fa66c7c2902ceb57e369d3cecf1a28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as fs from 'fs';
import xlsx from "node-xlsx";
 
const books = [];
 
function main() {
  // 获取所有日志文件
  const logFiles = fs.readdirSync('./book-isbn-logs', { withFileTypes: true });
  // 遍历日志文件
  for (const file of logFiles) {
    // 读取日志文件
    const log = fs.readFileSync(`./book-isbn-logs/${file.name}`, 'utf8');
    // 解析日志文件
    const bookLogs = log.split('开始下载');
    for (const bookLog of bookLogs) {
      const lines = bookLog.split('\n');
      // 遍历日志行
      for (const line of lines) {
        // 解析日志行
        let reg, group;
        reg = /.* ISBN: (.*)$/g;
        group = reg.exec(line);
        if (group) {
          const book = {};
          const text = group[1];
          const obj = JSON.parse(text);
          Object.assign(book, obj);
          books.push(book);
          continue;
        }
      }
 
    }
  }
}
 
try {
  main();
} catch (e) {
  console.error(e);
} finally {
  const EXCEL_FILE = "fiction-noisbn.xlsx";
  const workSheets = xlsx.parse(EXCEL_FILE);
  const sheet = workSheets[0];
  for (const book of books) {
    const row=sheet.data.find(row => row[0] == book.id);
    row[5] = book.isbn;
  }
  const buffer = xlsx.build([sheet]);
  fs.writeFileSync(EXCEL_FILE, buffer, (err) => { });
  console.log("保存完成: ", EXCEL_FILE);
}