diff --git a/docz/data/engines.xls b/docz/data/engines.xls index 7ee0540..3fe2df7 100644 --- a/docz/data/engines.xls +++ b/docz/data/engines.xls @@ -327,6 +327,16 @@ + + V8 + C# + + + + + + + JSC Swift diff --git a/docz/docs/03-demos/42-engines/02-v8.md b/docz/docs/03-demos/42-engines/02-v8.md index 83095d7..431801b 100644 --- a/docz/docs/03-demos/42-engines/02-v8.md +++ b/docz/docs/03-demos/42-engines/02-v8.md @@ -1123,6 +1123,165 @@ java -cp ".;javet-3.1.3.jar" SheetJSJavet pres.xlsx If the program succeeded, the CSV contents will be printed to console. +### C# + +ClearScript provides a .NET interface to the V8 engine. + +C# byte arrays (`byte[]`) must be explicitly converted to arrays of bytes: + +```csharp +/* read data into a byte array */ +byte[] filedata = File.ReadAllBytes("pres.numbers"); + +// highlight-start +/* generate a JS Array (variable name `buf`) from the data */ +engine.Script.buf = engine.Script.Array.from(filedata); +// highlight-end + +/* parse data */ +engine.Evaluate("var wb = XLSX.read(buf, {type: 'array'});"); +``` + +:::note Tested Deployments + +This demo was last tested in the following deployments: + +| Architecture | V8 Version | Date | +|:-------------|:--------------|:-----------| +| `darwin-x64` | `12.3.219.12` | 2024-07-16 | +| `darwin-arm` | `12.3.219.12` | 2024-07-16 | +| `win10-x64` | `12.3.219.12` | 2024-07-16 | +| `win11-arm` | `12.3.219.12` | 2024-07-16 | +| `linux-x64` | `12.3.219.12` | 2024-07-16 | +| `linux-arm` | `12.3.219.12` | 2024-07-16 | + +::: + +0) Set the `DOTNET_CLI_TELEMETRY_OPTOUT` environment variable to `1`. + +
+ How to disable telemetry (click to hide) + + + + +Add the following line to `.profile`, `.bashrc` and `.zshrc`: + +```bash title="(add to .profile , .bashrc , and .zshrc)" +export DOTNET_CLI_TELEMETRY_OPTOUT=1 +``` + +Close and restart the Terminal to load the changes. + + + + +Type `env` in the search bar and select "Edit the system environment variables". + +In the new window, click the "Environment Variables..." button. + +In the new window, look for the "System variables" section and click "New..." + +Set the "Variable name" to `DOTNET_CLI_TELEMETRY_OPTOUT` and the value to `1`. + +Click "OK" in each window (3 windows) and restart your computer. + + + + +
+ +1) Install .NET + +
+ Installation Notes (click to show) + +For macOS x64 and ARM64, install the `dotnet-sdk` Cask with Homebrew: + +```bash +brew install --cask dotnet-sdk +``` + +For Steam Deck Holo and other Arch Linux x64 distributions, the `dotnet-sdk` and +`dotnet-runtime` packages should be installed using `pacman`: + +```bash +sudo pacman -Syu dotnet-sdk dotnet-runtime +``` + +https://dotnet.microsoft.com/en-us/download/dotnet/6.0 is the official source +for Windows and ARM64 Linux versions. + +
+ +2) Open a new Terminal window in macOS or PowerShell window in Windows. + +3) Create a new project: + +```bash +mkdir SheetJSClearScript +cd SheetJSClearScript +dotnet new console +dotnet run +``` + +4) Add ClearScript to the project: + +```bash +dotnet add package Microsoft.ClearScript.Complete --version 7.4.5 +``` + +5) Download the SheetJS standalone script and test file. Move all three files to +the project directory: + + + +{`\ +curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js +curl -LO https://docs.sheetjs.com/pres.xlsx`} + + +6) Replace `Program.cs` with the following: + +```csharp title="Program.cs" +using Microsoft.ClearScript.JavaScript; +using Microsoft.ClearScript.V8; + +/* initialize ClearScript */ +var engine = new V8ScriptEngine(); + +/* Load SheetJS Scripts */ +engine.Evaluate(File.ReadAllText("xlsx.full.min.js")); +Console.WriteLine("SheetJS version {0}", engine.Evaluate("XLSX.version")); + +/* Read and Parse File */ +byte[] filedata = File.ReadAllBytes(args[0]); +engine.Script.buf = engine.Script.Array.from(filedata); +engine.Evaluate("var wb = XLSX.read(buf, {type: 'array'});"); + +/* Print CSV of first worksheet */ +engine.Evaluate("var ws = wb.Sheets[wb.SheetNames[0]];"); +var csv = engine.Evaluate("XLSX.utils.sheet_to_csv(ws)"); +Console.Write(csv); + +/* Generate XLSB file and save to SheetJSJint.xlsb */ +var xlsb = (ITypedArray)engine.Evaluate("XLSX.write(wb, {bookType: 'xlsb', type: 'buffer'})"); +File.WriteAllBytes("SheetJSClearScript.xlsb", xlsb.ToArray()); +``` + +After saving, run the program and pass the test file name as an argument: + +```bash +dotnet run pres.xlsx +``` + +If successful, the program will print the contents of the first sheet as CSV +rows. It will also create `SheetJSClearScript.xlsb`, a workbook that can be +opened in a spreadsheet editor. + ## Snapshots At a high level, V8 snapshots are raw dumps of the V8 engine state. It is much diff --git a/package.json b/package.json index 5e5b6dc..9a7ed84 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,8 @@ { "private": true, + "scripts": { + "dev": "cd docz && npm run start -- --host=0.0.0.0 --no-open" + }, "alex": { "allow": [ "host-hostess",