docs.sheetjs.com/tests/cli/txiki.ps1

56 lines
1.7 KiB
PowerShell
Raw Normal View History

2026-01-29 04:58:19 +00:00
#!/usr/bin/env pwsh
# https://docs.sheetjs.com/docs/demos/cli/txiki
$oldDir = Get-Location
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-txiki"
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir | Out-Null
Set-Location -Path $tempDir
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pres.numbers" -OutFile "pres.numbers"
@'
const XLSX = require("./xlsx.full.min");
/* tjs.args[1] is the first argument to the script */
const filename = tjs.args[1];
/* read and parse file */
const data = await tjs.readFile(filename);
const wb = XLSX.read(data);
/* generate CSV of first sheet */
const ws = wb.Sheets[wb.SheetNames[0]];
const csv = XLSX.utils.sheet_to_csv(ws);
/* print to terminal */
console.log(csv);
'@ | Out-File -FilePath "sheet2csv.js" -Encoding utf8
Invoke-WebRequest -Uri "https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js" -OutFile "xlsx.full.min.js"
$txikiUrl = "https://github.com/saghul/txiki.js/releases/download/v24.12.0/txiki-windows-x86_64.zip"
$txikiZip = "txiki-windows-x86_64.zip"
Invoke-WebRequest -Uri $txikiUrl -OutFile $txikiZip
# Extract the zip file
Expand-Archive -Path $txikiZip -DestinationPath "."
# Move contents from subdirectory to current directory
$txikiDir = Get-ChildItem -Directory -Filter "txiki-windows-x86_64*" | Select-Object -First 1
if ($txikiDir) {
Get-ChildItem -Path $txikiDir.FullName -File | Move-Item -Destination "."
Remove-Item -Path $txikiDir.FullName -Recurse -Force
}
# Bundle the script
npx -y esbuild sheet2csv.js --bundle --outfile=bundle.js --platform=neutral
# Compile and run
.\tjs compile bundle.js sheet2csv
.\sheet2csv pres.numbers
# Cleanup
Set-Location $oldDir
Remove-Item -Path $tempDir -Recurse -Force