Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views2 pages

2 Write Data DNTT

The document contains a script for an Excel workbook that transfers data from a source to a worksheet named 'Phieu chi'. It checks for existing data, updates it if necessary, and appends new rows based on specific conditions. The script also manages unique document numbers and formats data appropriately before writing it to the worksheet.

Uploaded by

vdc241221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

2 Write Data DNTT

The document contains a script for an Excel workbook that transfers data from a source to a worksheet named 'Phieu chi'. It checks for existing data, updates it if necessary, and appends new rows based on specific conditions. The script also manages unique document numbers and formats data appropriately before writing it to the worksheet.

Uploaded by

vdc241221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// đổ dữ liệu tương ứng từ file DNTT -> rawdata

function main(workbook: ExcelScript.Workbook, sourceData: string[][]): void {


const sheet = workbook.getWorksheet("Phieu chi");
if (!sheet) throw new Error("Không tìm thấy sheet 'Phieu chi'.");

const usedRange = sheet.getUsedRange();


const existingData = usedRange ? usedRange.getValues() as string[][] : [];

const columnIndexes = {
ngayct: 0, // A
soct: 2, // C
nguoinhan: 5, // F
diachi: 6, // G
//lydochi: 7, // H
diengiai: 8, // I
diengiaihachtoan: 13, // N
sotien: 16, // K
sodenghitt: 51, // AZ
id: 52 // BA
};

const existingMap = new Map<string, { rowIndex: number, row: string[] }>();


const soDeNghiToSoChungTu = new Map<string, string>();
let maxSoChungTu = 0;
// lưu dữ liệu
for (let i = 1; i < existingData.length; i++) {
const row = existingData[i];
const id = (row[columnIndexes.id] ?? "").toString().trim();
const requestNo = (row[columnIndexes.sodenghitt] ?? "").toString().trim();
const docNo = (row[columnIndexes.soct] ?? "").toString().trim();

const key = `${requestNo}__${id}`;


if (id && requestNo) {
existingMap.set(key, { rowIndex: i, row });

if (docNo.startsWith("PC25")) {
const num = parseInt(docNo.substring(4));
if (!isNaN(num) && num > maxSoChungTu) {
maxSoChungTu = num;
}

if (!soDeNghiToSoChungTu.has(requestNo)) {
soDeNghiToSoChungTu.set(requestNo, docNo);
}
}
}
}

const rowsToAppend: (string | number | boolean)[][] = [];


const lastRow = existingData.length;

for (const row of sourceData) {


// truong (ly do chi)
const [ngayctRaw, nguoinhan, diachi, diengiai, sotien, sodenghitt, id] =
row;
if (!id || !sodenghitt) continue;

const key = `${sodenghitt}__${id}`;


const sotienFormatted = Number((sotien ?? "").replace(/,/g,
"")).toString();

let ngayct = ngayctRaw;


if (/^\d{2}\/\d{2}\/\d{4}$/.test(ngayctRaw)) {
const [d, m, y] = ngayctRaw.split("/");
ngayct = `${d}/${m}/${y}`;
}
// check soct + sinh tự động
let docNo: string;
if (soDeNghiToSoChungTu.has(sodenghitt)) {
docNo = soDeNghiToSoChungTu.get(sodenghitt)!;
} else {
maxSoChungTu++;
docNo = `PC25${maxSoChungTu.toString().padStart(4, "0")}`;
soDeNghiToSoChungTu.set(sodenghitt, docNo);
}
// đổ dữ liệu
const newRowData: (string | number | boolean)[] = [];
newRowData[columnIndexes.ngayct] = ngayct;
newRowData[columnIndexes.nguoinhan] = nguoinhan;
newRowData[columnIndexes.diachi] = diachi;
//newRowData[columnIndexes.lydochi] = lydochi;
newRowData[columnIndexes.diengiai] = diengiai;
newRowData[columnIndexes.diengiaihachtoan] = diengiai;
newRowData[columnIndexes.sotien] = sotienFormatted;
newRowData[columnIndexes.sodenghitt] = sodenghitt;
newRowData[columnIndexes.id] = id;
newRowData[columnIndexes.soct] = docNo;

if (existingMap.has(key)) {
const existing = existingMap.get(key)!;
const oldRow = existing.row;
// so sánh dữ liệu
const isDifferent =
oldRow[columnIndexes.ngayct]?.toString().trim() !== ngayct ||
oldRow[columnIndexes.nguoinhan]?.toString().trim() !== nguoinhan ||
oldRow[columnIndexes.diachi]?.toString().trim() !== diachi ||
//oldRow[columnIndexes.lydochi]?.toString().trim() !== lydochi ||
oldRow[columnIndexes.diengiai]?.toString().trim() !== diengiai ||
oldRow[columnIndexes.diengiaihachtoan]?.toString().trim() !==
diengiai ||
oldRow[columnIndexes.sotien]?.toString().replace(/,/g, "") !==
sotienFormatted ||
oldRow[columnIndexes.sodenghitt]?.toString().trim() !== sodenghitt;

if (isDifferent) {
sheet.getRangeByIndexes(existing.rowIndex, 0, 1,
53).setValues([newRowData]);
}
} else {
rowsToAppend.push(newRowData);
}
}

if (rowsToAppend.length > 0) {
sheet.getRangeByIndexes(lastRow, 0, rowsToAppend.length,
53).setValues(rowsToAppend);
}
}

You might also like