Add install-word-addins.ps1 script

This commit is contained in:
oom 2026-10-29 16:15:54 +07:00
parent b1449ab668
commit bc61f0fd2e
3 changed files with 157 additions and 31 deletions

View file

@ -15,7 +15,7 @@ export default function HomePage() {
const handleTmplSelect = (template: string, data: string) => { const handleTmplSelect = (template: string, data: string) => {
//console.log('Location Selected:', { template, data }); //console.log('Location Selected:', { template, data });
}; };
const handleInput = (e: ChangeEvent<HTMLInputElement>)=>{ const handleInput = (e: ChangeEvent<HTMLInputElement>) => {
// console.log(e.target) // console.log(e.target)
return true; return true;
} }
@ -35,10 +35,10 @@ export default function HomePage() {
label1="Template(tid):" label2="ข้อมูล(fid):" onSelectChange={handleTmplSelect} /> label1="Template(tid):" label2="ข้อมูล(fid):" onSelectChange={handleTmplSelect} />
<div className="mb-2"></div> <div className="mb-2"></div>
<div> <div>
uid:<input type="text" name="uid" className="p-2 mb-2 border border-gray-300 rounded-md" defaultValue="john"/> uid:<input type="text" name="uid" className="p-2 mb-2 border border-gray-300 rounded-md" defaultValue="john" />
</div> </div>
<div> <div>
uname:<input type="text" name="uname" className="p-2 mb-2 border border-gray-300 rounded-md" defaultValue="John Doe"/> uname:<input type="text" name="uname" className="p-2 mb-2 border border-gray-300 rounded-md" defaultValue="John Doe" />
</div> </div>
<button <button
type="submit" type="submit"
@ -57,45 +57,56 @@ export default function HomePage() {
</div> </div>
<div className="p-4 border rounded-xl bg-white shadow-sm"> <div className="p-4 border rounded-xl bg-white shadow-sm">
<h2 className="font-semibold text-blue-600 mb-2">A Link Get(new)</h2> <h2 className="font-semibold text-blue-600 mb-2">A Link Get(new)</h2>
<Link href="/api/template/download-docx?tid=T_X&fid=T_no_warning&uid=john&uname=John%20Doe">T_no_warning_john_xxx.docx</Link><br/> <Link href="/api/template/download-docx?tid=T_X&fid=T_no_warning&uid=john&uname=John%20Doe">T_no_warning_john_xxx.docx</Link><br />
<Link href="/api/template/download-docx?tid=T_X&fid=TF_no_warning_P2&uid=john&uname=John%20Doe">TF_no_warning_P2_john_xxx.docx</Link><br/> <Link href="/api/template/download-docx?tid=T_X&fid=TF_no_warning_P2&uid=john&uname=John%20Doe">TF_no_warning_P2_john_xxx.docx</Link><br />
<Link href="/api/template/download-docx?tid=T_X&fid=TS_P2_IN&uid=john&uname=John%20Doe">TS_P2_IN_john_xxx.docx</Link><br/> <Link href="/api/template/download-docx?tid=T_X&fid=TS_P2_IN&uid=john&uname=John%20Doe">TS_P2_IN_john_xxx.docx</Link><br />
<Link href="/api/template/download-docx?tid=T12_X&fid=T12_b2_IN&uid=john&uname=John%20Doe">T12_b2_IN_john_xxx.docx</Link><br/> <Link href="/api/template/download-docx?tid=T12_X&fid=T12_b2_IN&uid=john&uname=John%20Doe">T12_b2_IN_john_xxx.docx</Link><br />
<Link href="/api/template/download-docx?tid=T12_X&fid=T12_FD155_3s-long&uid=john&uname=John%20Doe">T12_FD155_3s-long_john_xxx.docx</Link><br/> <Link href="/api/template/download-docx?tid=T12_X&fid=T12_FD155_3s-long&uid=john&uname=John%20Doe">T12_FD155_3s-long_john_xxx.docx</Link><br />
</div> </div>
<div className="p-4 border rounded-xl bg-white shadow-sm"> <div className="p-4 border rounded-xl bg-white shadow-sm">
<h2 className="font-semibold text-blue-600 mb-2">Template & Data</h2> <h2 className="font-semibold text-blue-600 mb-2">Template & Data</h2>
Template: <Link href="/tmpl/T_X.docx">T_X.docx</Link>, &nbsp; Template: <Link href="/tmpl/T_X.docx">T_X.docx</Link>, &nbsp;
<Link href="/tmpl/T12_X.docx">T12_X.docx</Link><br/> <Link href="/tmpl/T12_X.docx">T12_X.docx</Link><br />
Data: <Link href="tmpl/TF_no_warning_P2.json">TF_no_warning_P2.json</Link>, &nbsp; Data: <Link href="tmpl/TF_no_warning_P2.json">TF_no_warning_P2.json</Link>, &nbsp;
<Link href="/tmpl/T_no_warning.json">T_no_warning.json</Link>, &nbsp; <Link href="/tmpl/T_no_warning.json">T_no_warning.json</Link>, &nbsp;
<Link href="/tmpl/TS_P2_IN.json">TS_P2_IN.json</Link><br/> <Link href="/tmpl/TS_P2_IN.json">TS_P2_IN.json</Link><br />
<Link href="/tmpl/T12_b2_IN.json">T12_b2_IN.json</Link>, &nbsp; <Link href="/tmpl/T12_b2_IN.json">T12_b2_IN.json</Link>, &nbsp;
<Link href="/tmpl/T12_FD155_3s-long.json">T12_FD155_3s-long.json</Link> <Link href="/tmpl/T12_FD155_3s-long.json">T12_FD155_3s-long.json</Link>
</div> </div>
<div className="p-4 border rounded-xl bg-white shadow-sm"> <div className="p-4 border rounded-xl bg-white shadow-sm">
<h2 className="font-semibold text-blue-600 mb-2">Flow</h2> <h2 className="font-semibold text-blue-600 mb-2">Flow</h2>
<div className="text-xl font-bold text-center"> <div className="text-xl font-bold text-center">
หน้านี้(เลือกไฟล์)<br/> หน้านี้(เลือกไฟล์)<br />
&darr; <br/> &darr; <br />
download-docx(ตรวจสิทธิ์,ส่งเทมเพลต ID,ส่งข้อมูล)<br/> download-docx(ตรวจสิทธิ์,ส่งเทมเพลต ID,ส่งข้อมูล)<br />
&darr; <br/> &darr; <br />
generate(เปิดเทมเพลต,สร้าง docx)<br/> generate(เปิดเทมเพลต,สร้าง docx)<br />
&darr; <br/> &darr; <br />
ดาว์นโหลด docx เปิด MS Word อัตโนมัติ<br/> ดาว์นโหลด docx เปิด MS Word อัตโนมัติ<br />
&darr; <br/> &darr; <br />
Add-ins ตรวจสอบไฟล์, ผู้ใช้แก้ไข docx<br/> Add-ins ตรวจสอบไฟล์, ผู้ใช้แก้ไข docx<br />
&darr; <br/> &darr; <br />
Versioning(คุมเวอร์ชั่น)<br/> Versioning(คุมเวอร์ชั่น)<br />
&darr; <br/> &darr; <br />
ecourt(อัปเดตฐานข้อมูลและไฟล์)<br/> ecourt(อัปเดตฐานข้อมูลและไฟล์)<br />
</div>
</div>
</div>
<div className="p-8 max-w-md mx-auto space-y-8 bg-gray-50 min-h-screen">
<div className="p-10 max-w-md border rounded-xl bg-white shadow-sm">
<h2 className="font-semibold text-blue-600 mb-2">Addin</h2>
ติดตั้ง Word Add-ins ด้วยสคริปต์นี้ <br/>
<Link href="/wordaddin/install-word-addins.ps1"> install-word-addins.ps1 </Link>
<br/>
ติดติดปัญหาบน Power Shell ให้เรียกสองบรรทัดล่างก่อน <br />
<p className="whitespace-nowrap">Unblock-File .\install-word-addins.ps1</p>
<p className="whitespace-nowrap">Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force</p>
<p className="whitespace-nowrap">.\install-word-addins.ps1 -Install</p>
</div> </div>
</div> </div>
</div> </div>
</div>
); );
} }

View file

@ -0,0 +1,17 @@
import fs from "node:fs";
import path from "node:path";
import { requestOrigin } from "@/utils/request-origin";
// GET /wordaddin/install-word-addin.ps1 — สำหรับติดตั้ง Word add-in
const DEV_ORIGIN = "https://localhost:3000";
const TEMPLATE = path.join(process.cwd(),"app","wordaddin","install-word-addins.ps1", "template.ps1");
export function GET(req: Request): Response {
const template = fs.readFileSync(TEMPLATE, "utf8");
const manifest = template.replaceAll(DEV_ORIGIN, requestOrigin(req));
return new Response(manifest, {
headers: {
"content-type": "text/plain; charset=utf-8",
"Content-Disposition": "attachment; filename=install-word-addins.ps1",
"cache-control": "no-store",
},
});
}

View file

@ -0,0 +1,98 @@
# ติดตั้ง (sideload) / ถอด Word add-in "e-Court Documents Versioning" บน Windows
# เวอร์ชัน PowerShell แบบส่งให้เครื่องทดสอบที่ไม่มี Node/repo — คู่หูของ
# scripts/install-wordaddin.mjs (รันใน repo) และ scripts/install-wordaddin.sh (macOS)
# กลไกเดียวกันทั้งสามไฟล์: โหลด manifest จาก server เก็บไว้
# %USERPROFILE%\Documents\wordaddin-manifest\ แล้วจดทะเบียน registry
# HKCU\Software\Microsoft\Office\16.0\Wef\Developer (กลไกเดียวกับ office-addin-dev-settings)
#
# ใช้: powershell -ExecutionPolicy Bypass -File install-wordaddin.ps1 [-Url <manifest-url>] [-Remove]
# -Url default https://ecourt-note.chamomind.com/docedit/wordaddin/manifest.xml
# -Remove ถอดการติดตั้ง
#
# (ไฟล์นี้ต้องมี BOM — Windows PowerShell 5.1 อ่าน UTF-8 ไม่มี BOM เป็น ANSI แล้วข้อความไทยพัง)
# หลังลง/ถอดต้อง restart Word ทุกครั้ง · รายละเอียดเต็ม: docs/WORDADDIN-DEV.md
param(
[string]$Url = "https://localhost:3000/docedit/wordaddin/manifest.xml",
[switch]$Remove,
[switch]$Install
)
$ErrorActionPreference = "Stop"
# ให้ console พิมพ์ไทย/✓ ถูกต้อง และบังคับ TLS 1.2 (PS 5.1 บางเครื่อง default ยังเป็น TLS 1.0)
try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch { }
[Net.ServicePointManager]::SecurityProtocol = `
[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$Prefix = "[install-addin]"
$DevKeyCmd = "HKCU\Software\Microsoft\Office\16.0\Wef\Developer"
$ManifestDir = Join-Path $env:USERPROFILE "Documents\wordaddin-manifest"
$ManifestPath = Join-Path $ManifestDir "manifest.xml"
function Fail($message) {
Write-Host "$Prefix ✗ $message" -ForegroundColor Red
exit 1
}
if ($Remove) {
$Prefix = "[Uninstall-addin]"
Write-Host "$Prefix platform: win32 · โหมดถอดการติดตั้ง"
if (-not (Test-Path $ManifestPath)) {
Fail "ไม่พบ manifest ที่เก็บไว้ ($ManifestPath) จึงไม่รู้ GUID ของ add-in — ลบเองได้ที่: reg delete `"$DevKeyCmd`" /v <GUID> /f"
}
$stored = Get-Content -Raw -Encoding UTF8 $ManifestPath
if ($stored -notmatch "<Id>([0-9a-fA-F-]{36})</Id>") {
Fail "อ่าน GUID จาก $ManifestPath ไม่ได้"
}
$guid = $Matches[1].ToLower()
& reg.exe delete $DevKeyCmd /v $guid /f | Out-Null
if ($LASTEXITCODE -ne 0) {
Fail "reg delete ไม่สำเร็จ (exit $LASTEXITCODE) — ลบเองได้ที่: reg delete `"$DevKeyCmd`" /v $guid /f"
}
Remove-Item $ManifestPath -ErrorAction SilentlyContinue
Write-Host "$Prefix ✓ ถอดการติดตั้งแล้ว (registry $guid + ไฟล์ $ManifestPath)"
Write-Host "$Prefix ✓ เสร็จ"
exit 0
}
if ($Install) {
Write-Host "$Prefix platform: win32"
Write-Host "Install from $Url"
try {
$response = Invoke-WebRequest -Uri $Url -UseBasicParsing -TimeoutSec 30
} catch {
Fail "โหลด manifest จาก $Url ไม่ได้ ($_)"
}
$xml = $response.Content
if ($xml -notmatch "<OfficeApp") {
Fail "response จาก $Url ไม่ใช่ manifest ของ Office add-in (ไม่มี <OfficeApp)"
}
if ($xml -notmatch "<Id>([0-9a-fA-F-]{36})</Id>") {
Fail "หา <Id> (GUID) ใน manifest ไม่เจอจาก $Url"
}
$guid = $Matches[1].ToLower()
New-Item -ItemType Directory -Force -Path $ManifestDir | Out-Null
# เขียน UTF-8 ไม่มี BOM — ให้ manifest เหมือนต้นฉบับบน server เป๊ะ
[System.IO.File]::WriteAllText($ManifestPath, $xml, (New-Object System.Text.UTF8Encoding($false)))
Write-Host "$Prefix ✓ เก็บ manifest ไว้ที่ $ManifestPath"
& reg.exe add $DevKeyCmd /v $guid /t REG_SZ /d $ManifestPath /f | Out-Null
if ($LASTEXITCODE -ne 0) {
Fail "เขียน registry ไม่สำเร็จ (exit $LASTEXITCODE)"
}
Write-Host "$Prefix ✓ จดทะเบียน add-in ใน registry ($DevKeyCmd → $guid)"
Write-Host "$Prefix ✓ เสร็จ"
# Print-NextSteps
Write-Host ""
Write-Host "ขั้นต่อไป:"
Write-Host " 1. restart Word ให้ครบทุกหน้าต่าง (จำเป็นทุกครั้งหลังติดตั้ง/ถอนการติดตั้ง)"
Write-Host ' 2. เมนู Home > Add-ins> กดแล้วเลือก "e-Court Documents Versioning"'
Write-Host " 3. เปิดไฟล์ .docx ที่มี ticket ที่ยังไม่หมดอายุ เพื่อเริ่มทดสอบ"
exit 0
}
Write-Host "วิธีการใช้:"
Write-Host "ปิด Microsoft Word แล้วเรียกคำสั่งนี้"
Write-Host " install-word-addins [-Install] [-Remove] [-Url http://domain/manifest.yaml]"
Write-Host "ถ้าติดสิทธิ์ในการรันสคริปต์ให้เรียกคำสั่งนี้:"