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

@ -90,12 +90,23 @@ export default function HomePage() {
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 "ถ้าติดสิทธิ์ในการรันสคริปต์ให้เรียกคำสั่งนี้:"