JSK_Mechta/generate-ballots/scripts/create-ballots.js
2025-09-16 01:39:25 +03:00

168 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
TextRun,
patchDocument,
PatchType,
} from "https://esm.sh/docx@9.5.1";
function log(message)
{
const output_text = document.getElementById("output_text");
output_text.value = output_text.value + "\n" + message;
}
function progressStart()
{
const progressbar = document.getElementById("progressbar");
progressbar.classList.remove("hidden");
}
function progressDone()
{
const progressbar = document.getElementById("progressbar");
progressbar.classList.add("hidden");
}
function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const tmpLink = document.createElement("a");
tmpLink.style.display = "none";
tmpLink.href = url;
tmpLink.download = filename;
document.body.appendChild(tmpLink);
tmpLink.click();
document.body.removeChild(tmpLink);
URL.revokeObjectURL(url);
}
function sanitizeRecord(record)
{
const noData="нет данных";
for (var i = 0; i < record.length; ++i)
{
if (!record[i] || (""+record[i]).trim().toLowerCase() == noData)
{
record[i] = "";
}
else
{
record[i] = ""+record[i];
}
}
return record;
}
// Помещение (квартира) № {{APT_NUM}}
// СНИЛС {{SNILS}}
// Ф.И.О. собственника помещения {{FIO}}
// Документ, подтверждающий право собственности: {{DOCUMENT_NUM}}
// Дата документа: {{DOCUMENT_DATE}}
// Общая площадь квартиры: {{APT_AREA}}
// Размер доли в праве собственности: {{SHARE}}
async function populateBallot(template, record){
try {
record = sanitizeRecord(record);
const ballot = await patchDocument({
data: template,
outputType: "arraybuffer",
patches: {
APT_NUM: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[2])],
},
FIO: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[3])],
},
APT_AREA: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[5])],
},
SHARE: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[6])],
},
DOCUMENT_NUM: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[8])],
},
DOCUMENT_DATE: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[9] != "" ? ("от "+record[9]) : "")],
},
SNILS: {
type: PatchType.PARAGRAPH,
children: [ new TextRun(record[10])],
}
},
});
return ballot;
} catch (error) {
console.error(`Error: ${error}`);
}
};
async function generate()
{
progressStart();
let registry_file = document.getElementById("registry_file").files[0];
let template_file = await document.getElementById("template_file").files[0].arrayBuffer();
var dataset = await CSV.fetch({
file: registry_file,
delimiter: ";"
}
);
log("Реестр загружен.");
console.log(dataset);
var zip = new JSZip();
var ballots = {};
for (const r of dataset.records)
{
const name = "ballot_"+ r[2];
let filename = name + ".docx";
let discriminator = 1;
while(ballots[filename])
{
discriminator++;
filename = name + "_" + discriminator + ".docx";
}
ballots[filename] = (async () =>
{
var file = await populateBallot(template_file, r);
zip.file(filename, file);
})();
}
for (const b in ballots) await ballots[b];
var blob = await zip.generateAsync({type:"blob"});
downloadBlob(blob, "ballots.zip");
log("Готово!");
progressDone();
}
async function asyncMain() {
console.debug("in main");
let generateButton = document.getElementById("create_ballots_btn");
generateButton.onclick = generate;
}
if (document.readyState != "loading")
{
asyncMain();
}
else
{
window.onload = asyncMain;
}