Skip to content

Commit e7156e9

Browse files
authored
doc(UniverSheet): update sample code (#5502)
* doc: 增加保存按钮示例 * chore: 增加推送报表示例 * doc: 更新报表模板 * doc: 更新多语言文件 * feat: 增加保存文档示例 * refactor(Textarea): 重构代码 * refactor: 清空其他单元格 * refactor: 重构代码 * chore: 更新依赖包
1 parent b58502b commit e7156e9

File tree

7 files changed

+733
-19
lines changed

7 files changed

+733
-19
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<PackageReference Include="BootstrapBlazor.SummerNote" Version="9.0.3" />
6565
<PackageReference Include="BootstrapBlazor.TableExport" Version="9.2.1" />
6666
<PackageReference Include="BootstrapBlazor.Topology" Version="9.0.0" />
67-
<PackageReference Include="BootstrapBlazor.UniverSheet" Version="9.0.0-beta01" />
67+
<PackageReference Include="BootstrapBlazor.UniverSheet" Version="9.0.0-beta02" />
6868
<PackageReference Include="BootstrapBlazor.VideoPlayer" Version="9.0.3" />
6969
<PackageReference Include="BootstrapBlazor.WinBox" Version="9.0.7" />
7070
</ItemGroup>

src/BootstrapBlazor.Server/Components/Samples/UniverSheets.razor

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
<section ignore>
1515
<p>@((MarkupString)Localizer["NormalDesc1"].Value)</p>
1616
<Button OnClickWithoutRender="OnPushExcelData" Text="@Localizer["PushButtonText"]"></Button>
17+
<Button OnClickWithoutRender="OnSaveExcelData" Text="@Localizer["SaveButtonText"]"></Button>
1718
</section>
1819
<div class="bb-sheet-demo">
1920
<UniverSheet @ref="_sheetExcel" OnReadyAsync="OnReadyAsync"></UniverSheet>
2021
</div>
22+
<section ignore>
23+
<Textarea rows="3" readonly Value="@_jsonData"></Textarea>
24+
</section>
2125
</DemoBlock>
2226

2327
<DemoBlock Title="@Localizer["PluginTitle"]"
@@ -28,6 +32,6 @@
2832
<Button OnClickWithoutRender="OnPushPluginData" Text="@Localizer["PushButtonText"]"></Button>
2933
</section>
3034
<div class="bb-sheet-demo">
31-
<UniverSheet @ref="_sheetPlugin" Plugins="Plugins" OnPostDataAsync="OnPostDataAsync"></UniverSheet>
35+
<UniverSheet @ref="_sheetPlugin" Plugins="_plugins" OnPostDataAsync="OnPostDataAsync"></UniverSheet>
3236
</div>
3337
</DemoBlock>

src/BootstrapBlazor.Server/Components/Samples/UniverSheets.razor.cs

+38-12
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,45 @@ namespace BootstrapBlazor.Server.Components.Samples;
1010
/// </summary>
1111
public partial class UniverSheets
1212
{
13+
[Inject, NotNull]
14+
private IWebHostEnvironment? WebHost { get; set; }
15+
1316
[Inject, NotNull]
1417
private ToastService? ToastService { get; set; }
1518

1619
[Inject, NotNull]
1720
private IStringLocalizer<UniverSheets>? Localizer { get; set; }
1821

19-
private readonly Dictionary<string, string> Plugins = new()
22+
private readonly Dictionary<string, string> _plugins = new()
2023
{
2124
{ "ReportPlugin", "univer-sheet/plugin.js" }
2225
};
2326

24-
private UniverSheet _sheetExcel = default!;
27+
[NotNull]
28+
private UniverSheet? _sheetExcel = null;
29+
30+
[NotNull]
31+
private UniverSheet? _sheetPlugin = null;
2532

26-
private UniverSheet _sheetPlugin = default!;
33+
private static string? _reportData = null;
34+
35+
private string? _jsonData = null;
36+
37+
/// <summary>
38+
/// <inheritdoc/>
39+
/// </summary>
40+
protected override async Task OnInitializedAsync()
41+
{
42+
var reportFile = Path.Combine(WebHost.WebRootPath, "univer-sheet", "report.json");
43+
if (File.Exists(reportFile))
44+
{
45+
_reportData = await File.ReadAllTextAsync(reportFile);
46+
}
47+
}
2748

2849
private async Task OnReadyAsync() => await ToastService.Information(Localizer["ToastOnReadyTitle"], Localizer["ToastOnReadyContent"]);
2950

30-
private static Task<UniverSheetData?> OnPostDataAsync(UniverSheetData data)
51+
private static Task<UniverSheetData> OnPostDataAsync(UniverSheetData data)
3152
{
3253
// 这里可以根据 data 的内容进行处理然后返回处理后的数据
3354
// 本例返回与时间相关的数据
@@ -41,21 +62,26 @@ public partial class UniverSheets
4162
Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
4263
}
4364
};
44-
return Task.FromResult<UniverSheetData?>(result);
65+
return Task.FromResult(result);
4566
}
4667

4768
private async Task OnPushExcelData()
4869
{
4970
await _sheetExcel.PushDataAsync(new UniverSheetData()
5071
{
51-
MessageName = "MessageName",
52-
CommandName = "CommandName",
53-
Data = new object[]
54-
{
55-
new object[] { "1", "2", "3", "4", "5" },
56-
new object[] { "1", "2", "3", "4", "5" },
57-
}
72+
CommandName = "SetWorkbook",
73+
Data = _reportData
74+
});
75+
}
76+
77+
private async Task OnSaveExcelData()
78+
{
79+
var result = await _sheetExcel.PushDataAsync(new UniverSheetData()
80+
{
81+
CommandName = "GetWorkbook"
5882
});
83+
_jsonData = result?.Data?.ToString();
84+
StateHasChanged();
5985
}
6086

6187
private async Task OnPushPluginData()

src/BootstrapBlazor.Server/Locales/en-US.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7009,9 +7009,10 @@
70097009
"UniverSheetIntro": "Encapsulates the core spreadsheet component of the open source office suite ​Univer​, providing a high-performance, scalable online spreadsheet solution",
70107010
"NormalTitle": "Basic usage",
70117011
"NormalIntro": "Set your own plugins by setting the <code>Plugins</code> parameter",
7012-
"NormalDesc1": "Push data to the spreadsheet by calling the instance method <code>PushDataAsync</code>",
7012+
"NormalDesc1": "Push data to the spreadsheet by calling the instance method <code>PushDataAsync</code>. Click the <b>Save Data</b> button to get the serialized data of the table data",
70137013
"NormalDesc2": "Click the <b>Push Data</b> button to push data to the table, and click the first icon on the <b>Toolbar</b> to get data from the server.",
70147014
"PushButtonText": "Push",
7015+
"SaveButtonText": "Save",
70157016
"ToastOnReadyTitle": "Notification",
70167017
"ToastOnReadyContent": "The sheet is ready for push data.",
70177018
"PluginTitle": "Plugins",

src/BootstrapBlazor.Server/Locales/zh-CN.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7009,9 +7009,10 @@
70097009
"UniverSheetIntro": "封装开源办公套件 ​Univer​ 的核心电子表格组件,提供高性能、可扩展的在线表格解决方案",
70107010
"NormalTitle": "基础用法",
70117011
"NormalIntro": "通过调用实例方法 <code>PushDataAsync</code> 推送数据到电子表格",
7012-
"NormalDesc1": "点击 <b>推送数据</b> 按钮主动将数据推送给表格",
7012+
"NormalDesc1": "点击 <b>推送数据</b> 按钮主动将数据推送给表格,点击 <b>保存数据</b> 按钮获得表格数据序列化的数据",
70137013
"NormalDesc2": "点击 <b>推送数据</b> 按钮主动将数据推送给表格,点击 <b>工具栏</b> 第一个小按钮主动从服务器端获取数据",
70147014
"PushButtonText": "推送数据",
7015+
"SaveButtonText": "保存数据",
70157016
"ToastOnReadyTitle": "组件通知",
70167017
"ToastOnReadyContent": "表格组件已就绪,可进行后续数据推送等操作",
70177018
"PluginTitle": "自定义插件",

src/BootstrapBlazor.Server/wwwroot/univer-sheet/controller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const GetDataOperation = {
1414
});
1515
if (data) {
1616
const univerAPI = dataService.getUniverSheet().univerAPI;
17-
const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange(0, 0, 2, 1)
17+
const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange(0, 0, 2, 3)
1818
const defaultData = [
19-
[{ v: data.data.key }],
20-
[{ v: data.data.value }]
19+
[{ v: data.data.key }, { v: null }, { v: null }],
20+
[{ v: data.data.value }, { v: null }, { v: null }]
2121
]
2222
range.setValues(defaultData);
2323
}

0 commit comments

Comments
 (0)