[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
SpreadJS支持双向数据绑定,可以快速实现填报和报表需求。
在报表中,展示图片也是常见需求,那么如何将数据字段中的图片链接展示为图片呢?
目前SpreadJS可以通过浮动图片,单元格背景以及V14Update1中新增的图片迷你图三种方式显示图片。再结合一定的开发,即可实现绑定图片的需求。
参考图片迷你图实例,将含有链接的列隐藏,展示图片单元格使用IMAGE引用隐藏单元格。
优点:不需要代码开发,只要在设计模板时使用些小技巧即可
缺点:模板设计人员需要学习设计方法,合理安排隐藏单元格位置;Excel不支持函数迷你图,导出Excel显示#NAME(如果想要Excel显示也能正确,可以参考复选框导出的Demo进行导出时的逻辑设计)
var dataAddress = {
grapecityPic:'https://www.grapecity.com.cn/en/favicon/favicon-32x32.png',
baiduPic:'https://www.baidu.com/favicon.ico',
biligamePic:'https://s1.hdslb.com/bfs/seed/game-web/duang/header/images/biligame-logo-w200.e54786d.svg'
}
let source = new GC.Spread.Sheets.Bindings.CellBindingSource(dataAddress);
sheet.setDataSource(source);
在模板设计时,给单元格tag加上特殊标记,数据绑定完成后遍历单元格,查找标记,在单元格位置插入浮动图片。
优点:浮动图片可以导出Excel
缺点:模板设计时候要打标记;插入浮动图片需要考虑合并单元格的影响
// 绑定数据
var dataAddress = {
grapecityPic1: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAABTVBMVEUAAABROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnT////Bq8QHAAAAbXRSTlMAAAYUHAUzerPS3tN4LwQqm+vokiMBEkVATdjQQxFCiMv3yBVM5un44Mn08ksl855IKCmk9ZmK8WcHceONA5iJRPbqMDrwhV7+EBbVfQ0PhPxVQYfKPRfkgs4gU8XlRhrGSgI/J5cye7Td0bETX5wkLQAAAAFiS0dEbiIPURcAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfgDA4TLwWDqfrbAAABOklEQVQ4y2NgGG6AkZGJmYWFmYmREbs0Kxs7BycXFzcHDy8fIxbd/AKCuVAgJCwCNIVRVExcQhQmLyoplYsEpGUYZeXkFRSVlGH6VVTBEopqaupghoaAphaQ0taBKtDlBonq6RsYGukYmyAZZQo1wAzEMbdgBTqO0dJKC6HAGqKABewAG1uw4xmZ7GDSekr2EAUOjkCekzMj1DwXV5CslpKbhCzEF4zuHkABBU+o7xm9vEEKfHwZYTr8/HMxFQQEQriiskHBIWB3o1mhFwrh2mvqwZyE6kjOMIiCcISXXFG8GQF1gT5SqCAHVGQU1EXR4BCNEYhFDWrrOFikx0ubJESE+okmeiNHlrcBPFEkySX7geKWMSXVCSatlpqCJdEwsqalZ3BzZWZl8ziz4khTjEzKLCw5uJLcUAYANudwsxxyH+EAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMTItMTRUMTk6NDc6MDUrMDE6MDDo13SbAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTEyLTE0VDE5OjQ3OjA1KzAxOjAwmYrMJwAAAFd6VFh0UmF3IHByb2ZpbGUgdHlwZSBpcHRjAAB4nOPyDAhxVigoyk/LzEnlUgADIwsuYwsTIxNLkxQDEyBEgDTDZAMjs1Qgy9jUyMTMxBzEB8uASKBKLgDqFxF08kI1lQAAAABJRU5ErkJggg==',
grapecityPic2: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAABTVBMVEUAAABROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnT////Bq8QHAAAAbXRSTlMAAAYUHAUzerPS3tN4LwQqm+vokiMBEkVATdjQQxFCiMv3yBVM5un44Mn08ksl855IKCmk9ZmK8WcHceONA5iJRPbqMDrwhV7+EBbVfQ0PhPxVQYfKPRfkgs4gU8XlRhrGSgI/J5cye7Td0bETX5wkLQAAAAFiS0dEbiIPURcAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfgDA4TLwWDqfrbAAABOklEQVQ4y2NgGG6AkZGJmYWFmYmREbs0Kxs7BycXFzcHDy8fIxbd/AKCuVAgJCwCNIVRVExcQhQmLyoplYsEpGUYZeXkFRSVlGH6VVTBEopqaupghoaAphaQ0taBKtDlBonq6RsYGukYmyAZZQo1wAzEMbdgBTqO0dJKC6HAGqKABewAG1uw4xmZ7GDSekr2EAUOjkCekzMj1DwXV5CslpKbhCzEF4zuHkABBU+o7xm9vEEKfHwZYTr8/HMxFQQEQriiskHBIWB3o1mhFwrh2mvqwZyE6kjOMIiCcISXXFG8GQF1gT5SqCAHVGQU1EXR4BCNEYhFDWrrOFikx0ubJESE+okmeiNHlrcBPFEkySX7geKWMSXVCSatlpqCJdEwsqalZ3BzZWZl8ziz4khTjEzKLCw5uJLcUAYANudwsxxyH+EAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMTItMTRUMTk6NDc6MDUrMDE6MDDo13SbAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTEyLTE0VDE5OjQ3OjA1KzAxOjAwmYrMJwAAAFd6VFh0UmF3IHByb2ZpbGUgdHlwZSBpcHRjAAB4nOPyDAhxVigoyk/LzEnlUgADIwsuYwsTIxNLkxQDEyBEgDTDZAMjs1Qgy9jUyMTMxBzEB8uASKBKLgDqFxF08kI1lQAAAABJRU5ErkJggg==',
grapecityPic3: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAABTVBMVEUAAABROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnRROnT////Bq8QHAAAAbXRSTlMAAAYUHAUzerPS3tN4LwQqm+vokiMBEkVATdjQQxFCiMv3yBVM5un44Mn08ksl855IKCmk9ZmK8WcHceONA5iJRPbqMDrwhV7+EBbVfQ0PhPxVQYfKPRfkgs4gU8XlRhrGSgI/J5cye7Td0bETX5wkLQAAAAFiS0dEbiIPURcAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfgDA4TLwWDqfrbAAABOklEQVQ4y2NgGG6AkZGJmYWFmYmREbs0Kxs7BycXFzcHDy8fIxbd/AKCuVAgJCwCNIVRVExcQhQmLyoplYsEpGUYZeXkFRSVlGH6VVTBEopqaupghoaAphaQ0taBKtDlBonq6RsYGukYmyAZZQo1wAzEMbdgBTqO0dJKC6HAGqKABewAG1uw4xmZ7GDSekr2EAUOjkCekzMj1DwXV5CslpKbhCzEF4zuHkABBU+o7xm9vEEKfHwZYTr8/HMxFQQEQriiskHBIWB3o1mhFwrh2mvqwZyE6kjOMIiCcISXXFG8GQF1gT5SqCAHVGQU1EXR4BCNEYhFDWrrOFikx0ubJESE+okmeiNHlrcBPFEkySX7geKWMSXVCSatlpqCJdEwsqalZ3BzZWZl8ziz4khTjEzKLCw5uJLcUAYANudwsxxyH+EAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMTItMTRUMTk6NDc6MDUrMDE6MDDo13SbAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTEyLTE0VDE5OjQ3OjA1KzAxOjAwmYrMJwAAAFd6VFh0UmF3IHByb2ZpbGUgdHlwZSBpcHRjAAB4nOPyDAhxVigoyk/LzEnlUgADIwsuYwsTIxNLkxQDEyBEgDTDZAMjs1Qgy9jUyMTMxBzEB8uASKBKLgDqFxF08kI1lQAAAABJRU5ErkJggg=='
}
let source = new GC.Spread.Sheets.Bindings.CellBindingSource(dataAddress);
sheet.setDataSource(source);
// 数据转图片
var x = 0;
var y = 0;
var rowCount = sheet.getRowCount();
var colCount = sheet.getColumnCount();
for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < colCount; j++) {
// 当前单元格信息
var cellWidth = sheet.getCell(-1, j).width();
var cellHeight = sheet.getCell(i, -1).height();
var tag = sheet.getRange(i, j).tag();
if (tag === "image") {
// 插入浮动图片
console.log(i, j, tag);
var picAddress = sheet.getCell(i, j).value()
console.log(i, j, picAddress);
var picture = sheet.shapes.addPictureShape("pic" + i + "," + j, picAddress, x, y, cellWidth, cellHeight);
// sheet.getCell(i, j).value(undefined);
}
x += cellWidth;
if (j == colCount - 1) {
x = 0;
y += cellHeight;
}
}
}
通过设置单元格格式的方式将绑定的值转换为图片,其本质和本文介绍的第一种方法一样,但是不用隐藏单元格
优点:使用单元格类型设置图片更直观,和Checkbox等方式一致。
缺点:这种实现方式简单,但与Excel不兼容,图片导出Excel之后展示的是一个链接。
var dataAddress = {
grapecityPic:'https://www.grapecity.com.cn/en/favicon/favicon-32x32.png',
baiduPic:'https://www.baidu.com/favicon.ico',
biligamePic:'https://s1.hdslb.com/bfs/seed/game-web/duang/header/images/biligame-logo-w200.e54786d.svg'
}
let source = new GC.Spread.Sheets.Bindings.CellBindingSource(dataAddress);
sheet.setDataSource(source);
自定义图片单元格,在自定义单元格 paint
实现中设置参数的style
添加 backgroundImage 属性值为单元格 value
,value
参数传递空。在单元格中显示图片
优点:使用单元格类型设置图片更直观,和Checkbox
等方式一致。
缺点:单元格类型无法导出 Excel,因为value
参数传空且 SpreadJS 的数据绑定是双向的,因此会修改数据源
核心代码:
ImageCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
GC.Spread.Sheets.CellTypes.Base.prototype.paint.call(this, ctx, value, x, y, w, h, style, options)
let { sheet, row, col } = options
if (sheet.getTag(row, col) !== 'painted' && value) {// 控制重绘方法的范围,如果单元格被设置成了“painted”或者图片链接没有给则不会执行该方法
if (!(sheet.shapes.get(this.picType))) {
// 添加图片背景
var style = new GC.Spread.Sheets.Style();
// style.backColor = "lightgreen";
style.backgroundImage = value;
sheet.setStyle(row, col, style, GC.Spread.Sheets.SheetArea.viewport);
// 将表面文字置空
sheet.setValue(row, col, '')
} else {
let pic = sheet.shapes.get(this.picType)
pic.src(value)
}
sheet.setTag(row, col, 'painted')// 防止反复绘制
}
}
该方法其实和咱们介绍的第四种方式是一样的,只是我们将设定背景图片的操作改成了绘制浮动图片
优点:可以导出Excel,且不用双for循环导致性能的损耗
缺点:实现要求高
核心代码:
ImageCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
GC.Spread.Sheets.CellTypes.Base.prototype.paint.call(this, ctx, value, x, y, w, h, style, options)
let { sheet, row, col } = options
if (sheet.getTag(row, col) !== 'painted' && value) {// 控制重绘方法的范围,如果单元格被设置成了“painted”或者图片链接没有给则不会执行该方法
if (!(sheet.shapes.get(this.picType))) {
// 添加浮动图片
let pic = sheet.shapes.addPictureShape(this.picType, value, 0, 0, 100, 100)
// 如果判断单元格是否是合并的单元格
let span = sheet.getSpan(row, col)
let rowCount = 1
let colCount = 1
if (span) {
rowCount = span.rowCount
colCount = span.colCount
}
// 浮动图片起始行
pic.startRow(row)
// 浮动图片结束行
pic.endRow(row + rowCount)
// 浮动图片起始列
pic.startColumn(col)
// 浮动图片结束列
pic.endColumn(col + colCount)
// 禁止图片移动
pic.allowMove(false)
} else {
let pic = sheet.shapes.get(this.picType)
pic.src(value)
}
sheet.setTag(row, col, 'painted')// 防止反复绘制
}
}
以上是五种添加图片绑定的方法
完整版Demo可以附件:
作者: GrapeCity China | 审核:AlexZ | 更新时间:2023.09.24