Anonymous
Попытка обновить документ Word, введя данные на HTML -форму
Сообщение
Anonymous » 03 май 2025, 20:34
Я пытаюсь загрузить документ Word, и я пытаюсь обновить свои данные о моей онлайн -форме HTML, а затем после того, как данные были вводятся, я хочу, чтобы данные документа Word изменили в соответствии с тем, что было вводим.
Код: Выделить всё
document.addEventListener("DOMContentLoaded", function () {
// Ensure Mammoth.js is loaded
if (typeof Mammoth === "undefined") {
console.error("Mammoth.js is not loaded! Check script path.");
alert("Mammoth.js is not loaded! Check your internet connection.");
return;
}
document.getElementById("uploadDataBtn").addEventListener("click", modifyDocument);
});
async function modifyDocument() {
const fileInput = document.getElementById("fileUpload");
if (fileInput.files.length === 0) {
alert("Ju lutem ngarkoni një dokument DOCX.");
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = async function (event) {
const arrayBuffer = event.target.result;
try {
const result = await Mammoth.extractRawText({ arrayBuffer });
let docText = result.value;
console.log("Extracted Text:", docText); // Debugging
const replacements = {
"${name}": document.getElementById("name")?.value || "",
"${website}": document.getElementById("website")?.value || "",
"${responsible}": document.getElementById("responsible")?.value || "",
"${timeline}": document.getElementById("timeline")?.value || "",
"${description}": document.getElementById("description")?.value || "",
"${type}": document.getElementById("type")?.value || "",
"${time}": document.getElementById("time")?.value || "",
"${information}": document.getElementById("information")?.value || "",
"${referenca}": document.getElementById("referenca")?.value || "",
"${kodi}": document.getElementById("kodi")?.value || "",
};
// Replace placeholders with input values
for (const key in replacements) {
docText = docText.replace(new RegExp(key, "g"), replacements[key]);
}
console.log("Modified Text:", docText); // Debugging
// Create a new DOCX file
const doc = new docx.Document({
sections: [
{
properties: {},
children: [new docx.Paragraph({ text: docText })],
},
],
});
const blob = await docx.Packer.toBlob(doc);
saveAs(blob, "Updated_DST.docx");
} catch (error) {
console.error("Error processing document:", error);
alert("Gabim gjatë përpunimit të dokumentit.");
}
};
reader.readAsArrayBuffer(file);
}
Это мой код JS, но я не знаю, должен ли я спросить Chatgpt.
Подробнее здесь:
https://stackoverflow.com/questions/796 ... -html-form
1746293695
Anonymous
Я пытаюсь загрузить документ Word, и я пытаюсь обновить свои данные о моей онлайн -форме HTML, а затем после того, как данные были вводятся, я хочу, чтобы данные документа Word изменили в соответствии с тем, что было вводим.[code] document.addEventListener("DOMContentLoaded", function () { // Ensure Mammoth.js is loaded if (typeof Mammoth === "undefined") { console.error("Mammoth.js is not loaded! Check script path."); alert("Mammoth.js is not loaded! Check your internet connection."); return; } document.getElementById("uploadDataBtn").addEventListener("click", modifyDocument); }); async function modifyDocument() { const fileInput = document.getElementById("fileUpload"); if (fileInput.files.length === 0) { alert("Ju lutem ngarkoni një dokument DOCX."); return; } const file = fileInput.files[0]; const reader = new FileReader(); reader.onload = async function (event) { const arrayBuffer = event.target.result; try { const result = await Mammoth.extractRawText({ arrayBuffer }); let docText = result.value; console.log("Extracted Text:", docText); // Debugging const replacements = { "${name}": document.getElementById("name")?.value || "", "${website}": document.getElementById("website")?.value || "", "${responsible}": document.getElementById("responsible")?.value || "", "${timeline}": document.getElementById("timeline")?.value || "", "${description}": document.getElementById("description")?.value || "", "${type}": document.getElementById("type")?.value || "", "${time}": document.getElementById("time")?.value || "", "${information}": document.getElementById("information")?.value || "", "${referenca}": document.getElementById("referenca")?.value || "", "${kodi}": document.getElementById("kodi")?.value || "", }; // Replace placeholders with input values for (const key in replacements) { docText = docText.replace(new RegExp(key, "g"), replacements[key]); } console.log("Modified Text:", docText); // Debugging // Create a new DOCX file const doc = new docx.Document({ sections: [ { properties: {}, children: [new docx.Paragraph({ text: docText })], }, ], }); const blob = await docx.Packer.toBlob(doc); saveAs(blob, "Updated_DST.docx"); } catch (error) { console.error("Error processing document:", error); alert("Gabim gjatë përpunimit të dokumentit."); } }; reader.readAsArrayBuffer(file); } [/code] Это мой код JS, но я не знаю, должен ли я спросить Chatgpt. Подробнее здесь: [url]https://stackoverflow.com/questions/79604958/trying-to-update-word-document-by-inputting-data-ona-html-form[/url]