-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcelDemo.js
40 lines (30 loc) · 1.33 KB
/
excelDemo.js
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
const ExcelJs = require('exceljs');
async function writeExcelTest(searchText, replaceText, change, filePath){
const workbook = new ExcelJs.Workbook();
await workbook.xlsx.readFile(filePath);
const worksheet = workbook.getWorksheet('Sheet1'); // This needs to be declared before calling readExcel
// Call readExcel and wait for its completion
const output = await readExcel(worksheet, searchText, change);
if (output.row !== -1 && output.column !== -1) {
const cell = worksheet.getCell(output.row, output.column + change.colChange);
cell.value = replaceText;
await workbook.xlsx.writeFile(filePath);
console.log(`Replaced price for '${searchText}' to '${replaceText}' at row ${output.row}, column ${output.column}.`);
} else {
console.log(`Text '${searchText}' not found in the file.`);
}
}
async function readExcel(worksheet, searchText) {
let output = { row: -1, column: -1 };
worksheet.eachRow((row, rowNumber) => {
row.eachCell((cell, colNumber) => {
if (cell.value === searchText) {
output.row = rowNumber;
output.column = colNumber;
}
});
});
return output;
}
//update mango price to 350
writeExcelTest("Republic", 350, {rowChange: 0, colChange:2}, "./files/excellDownloadTest.xlsx");