ns-android

This commit is contained in:
SheetJS 2023-05-07 13:43:21 -04:00
parent 8c754069bf
commit 28570a8fbd
3 changed files with 66 additions and 29 deletions

View File

@ -721,38 +721,30 @@ The Android demo has been tested in Windows 10 and in macOS.
Test the app in the Android simulator:
```bash
npm run android
npx react-native start
```
Once Metro is ready, it will display the commands:
```
r - reload the app
d - open developer menu
i - run on iOS
a - run on Android
```
Press `a` to run on android.
After clicking "Press to Export", the app will show an alert with the location
to the generated file.
to the generated file (`/data/user/0/com.sheetjspres/files/Presidents.xlsx`)
In the Android simulator, pulling the file requires additional steps. This
command will pull a Base64-encoded string from the simulator:
In the Android simulator, pulling the file requires additional steps:
```bash
adb exec-out run-as com.sheetjspres base64 files/Presidents.xlsx > pres.b64
adb root
adb pull /data/user/0/com.sheetjspres/files/Presidents.xlsx Presidents.xlsx
```
Decoding the file requires an OS-specific command:
<Tabs>
<TabItem value="windows" label="Windows">
```powershell
certutil -decode .\pres.b64 .\Presidents.xlsx
```
</TabItem>
<TabItem value="macos" label="macOS">
```bash
base64 -D pres.b64 > Presidents.xlsx
```
</TabItem>
</Tabs>
This command generates `Presidents.xlsx` which can be opened.
:::info Device Testing

View File

@ -17,10 +17,15 @@ The "Complete Example" creates an app that looks like the screenshots below:
<table><thead><tr>
<th><a href="#demo">iOS</a></th>
<th><a href="#demo">Android</a></th>
</tr></thead><tbody><tr><td>
![iOS screenshot](pathname:///mobile/nsios.png)
</td><td>
![Android screenshot](pathname:///mobile/nsand.png)
</td></tr></tbody></table>
## Integration Details
@ -63,7 +68,8 @@ const wb = read(ab);
#### Writing data
`getFileAccess().writeBufferAsync` can write data:
`getFileAccess().writeBufferAsync` can write data. iOS supports `Uint8Array`
directly but Android requires a true array of numbers:
```ts
import { getFileAccess } from '@nativescript/core';
@ -75,15 +81,15 @@ const url = get_url_for_filename("SheetJSNS.xls");
const u8: Uint8Array = write(wb, { bookType: 'xls', type: 'binary' });
/* attempt to save Uint8Array to file */
await getFileAccess().writeBufferAsync(url, u8);
await getFileAccess().writeBufferAsync(url, global.isAndroid ? (Array.from(u8) as any) : u8);
```
## Demo
:::note
This demo was tested on an Intel Mac on 2023 April 03. NativeScript version
(as verified with `ns --version`) is `8.5.1`.
This demo was tested on an Intel Mac on 2023 May 07. NativeScript version
(as verified with `ns --version`) is `8.5.3`.
The iOS simulator runs iOS 16.2 on an iPhone 14 Pro Max.
@ -91,6 +97,8 @@ The iOS simulator runs iOS 16.2 on an iPhone 14 Pro Max.
0) Follow the official Environment Setup instructions
### Base Project
1) Create a skeleton NativeScript + Angular app:
```bash
@ -262,7 +270,7 @@ Restart the app process and two buttons should show up at the top:
const u8: Uint8Array = write(wb, { bookType: 'xls', type: 'buffer' });
/* attempt to save Uint8Array to file */
await getFileAccess().writeBufferAsync(url, u8);
await getFileAccess().writeBufferAsync(url, global.isAndroid ? (Array.from(u8) as any) : u8);
await Dialogs.alert(`Wrote to SheetJSNS.xls at ${url}`);
} catch(e) { await Dialogs.alert(e.message); }
// highlight-end
@ -296,3 +304,40 @@ Restart the app after saving the file.
![NativeScript Step 7](pathname:///mobile/nativescript7.png)
### Android
Launch the app with `ns run android`. If the app does not automatically launch,
manually open the `SheetJSNS` app.
The app can be tested with the following sequence in the simulator:
- Tap "Export File". A dialog will print where the file was written. Typicaly
the URL is `/data/user/0/org.nativescript.SheetJSNS/files/SheetJSNS.xls`
- Pull the file from the simulator:
```bash
adb root
adb pull /data/user/0/org.nativescript.SheetJSNS/files/SheetJSNS.xls SheetJSNS.xls
```
- Open `SheetJSNS.xls` with a spreadsheet editor.
After the header row, insert a row with cell A2 = 0, B2 = SheetJS, C2 = Library:
```
id | name | role
0 | SheetJS | Library
1 | Ter Stegen | Goalkeeper
3 | Piqué | Defender
...
```
- Push the file back to the simulator:
```bash
adb push SheetJSNS.xls /data/user/0/org.nativescript.SheetJSNS/files/SheetJSNS.xls
```
- Tap "Import File". A dialog will print the path of the file that was read.
The first item in the list will change.

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB