找回密码
 立即注册

QQ登录

只需一步,快速开始

Winny

超级版主

141

主题

260

帖子

1673

积分

超级版主

Rank: 8Rank: 8

积分
1673
Winny
超级版主   /  发表于:2022-6-2 21:35  /   查看:3131  /  回复:4
本帖最后由 Winny 于 2022-6-15 17:11 编辑

SpreadJS V14.2.0之后,组件运行时支持了Vue3,详细用法可参考Vue3中使用SpreadJS组件运行时

SpreadJSV15.1.0中,对于在线表格编辑器,新增了Vue3的支持,本文会演示如何在Vue3工程中引入在线表格编辑器。
Step1: 可以选择以下任意方式创建一个Vue3工程,在这里,我用的是vite,当然你也可以选择使用vue-cli;
  1. # npm 6.x
  2. npm init vite@latest spreadjs-vue-ts --template vue-ts

  3. # npm 7+, 需要额外的双横线:
  4. npm init vite@latest spreadjs-vue-ts -- --template vue-ts

  5. # yarn
  6. yarn create vite spreadjs-vue-ts --template vue-ts

  7. # pnpm
  8. pnpm create vite my-vue-app -- --template vue
复制代码
Step2: 安装相关资源
  1. npm install @grapecity/spread-sheets-designer
  2. npm install @grapecity/spread-sheets-designer-resources-cn
  3. npm install @grapecity/spread-sheets-designer-vue
  4. npm install @grapecity/spread-sheets
  5. npm install @grapecity/spread-excelio
  6. npm install @grapecity/spread-sheets-barcode
  7. npm install @grapecity/spread-sheets-charts
  8. npm install @grapecity/spread-sheets-languagepackages
  9. npm install @grapecity/spread-sheets-print
  10. npm install @grapecity/spread-sheets-pdf
  11. npm install @grapecity/spread-sheets-shapes
  12. npm install @grapecity/spread-sheets-pivot-addon
  13. npm install @grapecity/spread-sheets-tablesheet
复制代码
Step3: 组件注册
在main.js中对designer组件进行全局注册,当然你也可以使用局部组件注册方式,这里演示的是全局注册的方式。
  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import Designer from "@grapecity/spread-sheets-designer-vue"
  4. const app = createApp(App);
  5. app.component("gc-spread-sheets-designer",Designer);
  6. app.mount('#app')
复制代码

Step4:  创建vue文件,使用组件
在components文件夹下新建VueDesigner.vue,引入designer其它依赖资源,编写组件,详细代码如下所示:
  1. <template>
  2.   <div id="gc-designer-container">
  3.     <gc-spread-sheets-designer
  4.       :styleInfo="styleInfo"
  5.       :spreadOptions="spreadOptions"
  6.       @designerInitialized="designerInitialized"/>
  7.   </div>
  8. </template>

  9. <script setup lang="ts">
  10. import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css';
  11. import "@grapecity/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css";
  12. import * as GC from "@grapecity/spread-sheets";
  13. import "@grapecity/spread-sheets-print";
  14. import "@grapecity/spread-sheets-shapes";
  15. import "@grapecity/spread-sheets-pivot-addon";
  16. import "@grapecity/spread-sheets-tablesheet";
  17. import "@grapecity/spread-sheets-designer-resources-cn";
  18. import * as GcDesigner from "@grapecity/spread-sheets-designer";
  19. import {reactive, toRefs} from 'vue'
  20. interface IData{
  21.     designer:GcDesigner.Spread.Sheets.Designer.Designer|null,
  22.     spread: GC.Spread.Sheets.Workbook|null
  23. }
  24. const data:IData = reactive({
  25.      designer:null,
  26.      spread: null
  27. })
  28. const refData = toRefs(data)
  29. const styleInfo={height: '98vh'}
  30. const spreadOptions={sheetCount:2}
  31. const designerInitialized = (entity:GcDesigner.Spread.Sheets.Designer.Designer) => {
  32.      data.designer = entity
  33.      let spread:GC.Spread.Sheets.Workbook = data.designer.getWorkbook() as GC.Spread.Sheets.Workbook
  34.      let sheet = spread.getActiveSheet()
  35.      sheet.setValue(0,0,'Grapecity')
  36. }
  37. </script>
复制代码
**注意**这里使用Vue3提供的reactive方法对designer进行了封装,但是根据以往经验,这种封装后续会给数据添加一些额外的属性,种种操作会使ts验证报一些意向不到的错误。因此这里推荐还是使用直观的变量形式来定义designer。
step5: 在app.vue中引入定义好的vue-designer组件,代码如下:
  1. <template>
  2.   <vue-designer/>
  3. </template>

  4. <script setup lang="ts">
  5.   import VueDesigner from './components/VueDesigner.vue';
  6. </script>

  7. <style>
  8. #app {
  9.   font-family: Avenir, Helvetica, Arial, sans-serif;
  10.   -webkit-font-smoothing: antialiased;
  11.   -moz-osx-font-smoothing: grayscale;
  12.   color: #2c3e50;
  13. }
  14. </style>
复制代码

详细demo见文章末尾。


spreadjs-vue3-ts-vite.zip

21.61 KB, 下载次数: 578

4 个回复

倒序浏览
呆子
注册会员   /  发表于:2022-6-13 17:11:54
沙发
报错信息:
image.png224810179.png

main.js
  1. import { createApp } from "vue";
  2. import { createPinia } from "pinia";

  3. import ElementPlus from "element-plus";
  4. import "element-plus/dist/index.css";

  5. import App from "./App.vue";
  6. import router from "./router";

  7. // import {
  8. //   GcSpreadSheets,
  9. //   GcWorksheet,
  10. //   GcColumn,
  11. // } from "@grapecity/spread-sheets-vue";
  12. import Designer from "@grapecity/spread-sheets-designer-vue";
  13. // import "@/style/normalize.css";
  14. import "virtual:windi.css";

  15. const app = createApp(App);

  16. app.component("gc-spread-sheets-designer", Designer);
  17. // app.component("gc-spread-sheets", GcSpreadSheets);
  18. // app.component("gc-worksheet", GcWorksheet);
  19. // app.component("gc-column", GcColumn);

  20. app.use(ElementPlus);
  21. app.use(createPinia());
  22. app.use(router);

  23. app.mount("#app");
复制代码


// VueDesigner.vue
  1. <template>
  2.   <div id="gc-designer-container">
  3.     <gc-spread-sheets-designer
  4.       :styleInfo="styleInfo"
  5.       :spreadOptions="spreadOptions"
  6.       @designerInitialized="designerInitialized"
  7.     />
  8.   </div>
  9. </template>

  10. <script setup>
  11. import "@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css";
  12. import "@grapecity/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css";
  13. import * as GC from "@grapecity/spread-sheets";
  14. import "@grapecity/spread-sheets-print";
  15. import "@grapecity/spread-sheets-shapes";
  16. import "@grapecity/spread-sheets-pivot-addon";
  17. import "@grapecity/spread-sheets-tablesheet";
  18. import "@grapecity/spread-sheets-designer-resources-cn";
  19. import * as GcDesigner from "@grapecity/spread-sheets-designer";
  20. import { reactive, toRefs } from "vue";
  21. const data = reactive({
  22.   designer: GcDesigner.Spread.Sheets.Designer.Designer | null,
  23.   spread: GC.Spread.Sheets.Workbook | null,
  24. });
  25. const refData = toRefs(data);
  26. const styleInfo = { height: "98vh" };
  27. const spreadOptions = { sheetCount: 2 };
  28. const designerInitialized = (entity) => {
  29.   data.designer = entity || GcDesigner.Spread.Sheets.Designer.Designer;
  30.   let spread = data.designer.getWorkbook(); //  GC.Spread.Sheets.Workbook
  31.   let sheet = spread.getActiveSheet();
  32.   sheet.setValue(0, 0, "Grapecity");
  33. };
  34. </script>
复制代码


package.json
  1. "dependencies": {
  2.     "@fortawesome/fontawesome-free": "^6.1.1",
  3.     "@grapecity/spread-excelio": "^15.1.1",
  4.     "@grapecity/spread-sheets": "^15.1.1",
  5.     "@grapecity/spread-sheets-barcode": "^15.1.1",
  6.     "@grapecity/spread-sheets-charts": "^15.1.1",
  7.     "@grapecity/spread-sheets-designer": "^15.1.1",
  8.     "@grapecity/spread-sheets-designer-resources-cn": "^15.1.1",
  9.     "@grapecity/spread-sheets-designer-vue": "^15.1.1",
  10.     "@grapecity/spread-sheets-languagepackages": "^15.1.1",
  11.     "@grapecity/spread-sheets-pdf": "^15.1.1",
  12.     "@grapecity/spread-sheets-pivot-addon": "^15.1.1",
  13.     "@grapecity/spread-sheets-print": "^15.1.1",
  14.     "@grapecity/spread-sheets-resources-zh": "^15.1.0",
  15.     "@grapecity/spread-sheets-shapes": "^15.1.1",
  16.     "@grapecity/spread-sheets-tablesheet": "^15.1.1",
  17.     "@grapecity/spread-sheets-vue": "^15.1.0",
  18.     "axios": "^0.27.2",
  19.     "element-plus": "^2.2.2",
  20.     "pinia": "^2.0.14",
  21.     "vue": "^3.2.36",
  22.     "vue-router": "^4.0.15"
  23.   },
复制代码
回复 使用道具 举报
Winny
超级版主   /  发表于:2022-6-13 17:13:58
板凳

你在main.js里边把resources的资源加一下。
回复 使用道具 举报
snowflakes7
注册会员   /  发表于:2022-6-22 18:22:14
地板
本帖最后由 snowflakes7 于 2022-6-22 18:26 编辑
  1. <template>
  2.   <div id="gc-designer-container">
  3.     <gc-spread-sheets-designer
  4.       :styleInfo="styleInfo"
  5.       :spreadOptions="spreadOptions"
  6.       @designerInitialized="designerInitialized"
  7.     />
  8.   </div>
  9. </template>

  10. <script setup lang="ts">
  11. import "@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css";
  12. import "@grapecity/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css";
  13. import * as GC from "@grapecity/spread-sheets";
  14. import "@grapecity/spread-sheets-print";
  15. import "@grapecity/spread-sheets-shapes";
  16. import "@grapecity/spread-sheets-pivot-addon";
  17. import "@grapecity/spread-sheets-tablesheet";
  18. import "@grapecity/spread-sheets-designer-resources-cn";
  19. import * as GcDesigner from "@grapecity/spread-sheets-designer";
  20. import { reactive, toRefs, onMounted, ref } from "vue";

  21. // GC.Spread.Sheets.Designer.LicenseKey = "my key";


  22. interface IData {
  23.   designer: GcDesigner.Spread.Sheets.Designer.Designer | null;
  24.   spread: GC.Spread.Sheets.Workbook | null;
  25. }
  26. const data: IData = reactive({
  27.   designer: null,
  28.   spread: null,
  29. });
  30. const refData = toRefs(data);
  31. const styleInfo = { height: "98vh" };
  32. const spreadOptions = { sheetCount: 2 };
  33. const designerInitialized = (
  34.   entity: GcDesigner.Spread.Sheets.Designer.Designer
  35. ) => {
  36.   data.designer = entity;
  37.   let spread: GC.Spread.Sheets.Workbook =
  38.     data.designer.getWorkbook() as GC.Spread.Sheets.Workbook;
  39.   let sheet = spread.getActiveSheet();
  40.   sheet.setValue(0, 0, "Grapecity");
  41. };

  42. const addBar = () => {
  43.   var config = GC.Spread.Sheets.Designer.DefaultConfig;
  44.   config.commandMap = {
  45.     Welcome: {
  46.       title: "Welcome",
  47.       text: "Welcome",
  48.       iconClass: "ribbon-button-welcome",
  49.       bigButton: "true",
  50.       commandName: "Welcome",
  51.       execute: async (context, propertyName, fontItalicChecked) => {
  52.         alert("Welcome to new designer.");
  53.       },
  54.     },
  55.   };
  56.   config.ribbon[0].buttonGroups.unshift({
  57.     label: "NewDesigner",
  58.     thumbnailClass: "welcome",
  59.     commandGroup: {
  60.       children: [
  61.         {
  62.           direction: "vertical",
  63.           commands: ["Welcome"],
  64.         },
  65.         // This is custom button ----------------end-------------
  66.       ],
  67.     },
  68.   });
  69. };
  70. addBar();
  71. </script>
复制代码
设置licenseKey.jpg199613036.png 未设置licenseKey.png516510924.png
使用该 demo 添加了 自定义按钮;在未设置 LicenseKey是可以正常显示,在添加了LicenseKey之后,按钮不显示。这个要如何解决。
C:\Users\mujunhong\Desktop\未设置licenseKey.png
C:\Users\mujunhong\Desktop\设置licenseKey.png

设置licenseKey

设置licenseKey
回复 使用道具 举报
Winny
超级版主   /  发表于:2022-6-24 14:13:23
5#
snowflakes7 发表于 2022-6-22 18:22
使用该 demo 添加了 自定义按钮;在未设置 LicenseKey是可以正常显示,在添加了LicenseKey之后,按钮不显 ...

无法复现问题,请提供相关demo到求助专区发帖~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部