V8 C# demo
This commit is contained in:
parent
ffbe7af307
commit
06f9a2d0e7
@ -327,6 +327,16 @@
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
</Row>
|
||||
<Row>
|
||||
<Cell ss:StyleID="s20" ss:HRef="/docs/demos/engines/v8#c"><Data ss:Type="String">V8</Data></Cell>
|
||||
<Cell><Data ss:Type="String">C#</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
<Cell ss:StyleID="s16"><Data ss:Type="String">✔</Data></Cell>
|
||||
</Row>
|
||||
<Row>
|
||||
<Cell ss:StyleID="s20" ss:HRef="/docs/demos/engines/jsc#swift"><Data ss:Type="String">JSC</Data></Cell>
|
||||
<Cell><Data ss:Type="String">Swift</Data></Cell>
|
||||
|
@ -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`.
|
||||
|
||||
<details open>
|
||||
<summary><b>How to disable telemetry</b> (click to hide)</summary>
|
||||
|
||||
<Tabs groupId="os">
|
||||
<TabItem value="unix" label="Linux/MacOS">
|
||||
|
||||
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.
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="win" label="Windows">
|
||||
|
||||
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.
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
</details>
|
||||
|
||||
1) Install .NET
|
||||
|
||||
<details>
|
||||
<summary><b>Installation Notes</b> (click to show)</summary>
|
||||
|
||||
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.
|
||||
|
||||
</details>
|
||||
|
||||
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:
|
||||
|
||||
<ul>
|
||||
<li><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
|
||||
<li><a href="https://docs.sheetjs.com/pres.xlsx">pres.xlsx</a></li>
|
||||
</ul>
|
||||
|
||||
<CodeBlock language="bash">{`\
|
||||
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js
|
||||
curl -LO https://docs.sheetjs.com/pres.xlsx`}
|
||||
</CodeBlock>
|
||||
|
||||
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<byte>)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
|
||||
|
@ -1,5 +1,8 @@
|
||||
{
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "cd docz && npm run start -- --host=0.0.0.0 --no-open"
|
||||
},
|
||||
"alex": {
|
||||
"allow": [
|
||||
"host-hostess",
|
||||
|
Loading…
Reference in New Issue
Block a user