add import file product
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
This commit is contained in:
parent
fd7833a592
commit
05d16f22de
3 changed files with 236 additions and 0 deletions
105
src/utils/spreadsheet.ts
Normal file
105
src/utils/spreadsheet.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import Excel from "exceljs";
|
||||
|
||||
export default class spreadsheet {
|
||||
static async readCsv() {
|
||||
// TODO: read csv
|
||||
}
|
||||
|
||||
/**
|
||||
* This function read data from excel file.
|
||||
*
|
||||
* @param buffer - Excel file.
|
||||
* @param opts.header - Interprets the first row as the names of the fields.
|
||||
* @param opts.worksheet - Specifies the worksheet to read. Can be the worksheet's name or its 1-based index.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
static async readExcel<T extends unknown>(
|
||||
buffer: Excel.Buffer,
|
||||
opts?: { header?: boolean; worksheet?: number | string },
|
||||
): Promise<T[]> {
|
||||
const workbook = new Excel.Workbook();
|
||||
await workbook.xlsx.load(buffer);
|
||||
const worksheet = workbook.getWorksheet(opts?.worksheet ?? 1);
|
||||
|
||||
if (!worksheet) return [];
|
||||
|
||||
const header: Record<number, string | number> = {};
|
||||
const values: any[] = [];
|
||||
|
||||
worksheet.eachRow((row, rowId) => {
|
||||
if (rowId === 1 && opts?.header !== false) {
|
||||
row.eachCell((cell, cellId) => {
|
||||
if (typeof cell.value === "string") {
|
||||
header[cellId] = nameValue(cell.value);
|
||||
} else {
|
||||
header[cellId] = cellId.toString();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const data: Record<string | number, Excel.CellValue> = {};
|
||||
row.eachCell((cell, cellId) => {
|
||||
data[opts?.header !== false ? header[cellId] : cellId - 1] = cell.value;
|
||||
});
|
||||
values.push(opts?.header !== false ? data : Object.values(data));
|
||||
}
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
function nameValue(value: string) {
|
||||
let code: string;
|
||||
switch (value) {
|
||||
case "ชื่อสินค้าและบริการ":
|
||||
code = "name";
|
||||
break;
|
||||
case "ระยะเวลาดำเนินการ":
|
||||
code = "process";
|
||||
break;
|
||||
case "ประเภทค่าใช้จ่าย":
|
||||
code = "expenseType";
|
||||
break;
|
||||
case "รายละเอียด":
|
||||
code = "detail";
|
||||
break;
|
||||
case "หมายเหตุ":
|
||||
code = "remark";
|
||||
break;
|
||||
case "ใช้งานร่วมกัน":
|
||||
code = "shared";
|
||||
break;
|
||||
case "คำนวณภาษีราคาขาย":
|
||||
code = "calcVat";
|
||||
break;
|
||||
case "รวม VAT ราคาขาย":
|
||||
code = "vatIncluded";
|
||||
break;
|
||||
case "ราคาต่อหน่วย (บาท) ราคาขาย":
|
||||
code = "price";
|
||||
break;
|
||||
case "คำนวณภาษีราคาตัวแทน":
|
||||
code = "agentPriceCalcVat";
|
||||
break;
|
||||
case "รวม VAT ราคาตัวแทน":
|
||||
code = "agentPriceVatIncluded";
|
||||
break;
|
||||
case "ราคาต่อหน่วย (บาท) ราคาตัวแทน":
|
||||
code = "agentPrice";
|
||||
break;
|
||||
case "คำนวณภาษีราคาดำเนินการ":
|
||||
code = "serviceChargeCalcVat";
|
||||
break;
|
||||
case "รวม VAT ราคาดำเนินการ":
|
||||
code = "serviceChargeVatIncluded";
|
||||
break;
|
||||
case "ราคาต่อหน่วย (บาท) ราคาดำเนินการ":
|
||||
code = "serviceCharge";
|
||||
break;
|
||||
default:
|
||||
code = "code";
|
||||
break;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue