[{"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"}]}]
        
(Showing Draft Content)

边框样式优先级

问题:为什么我用代码设置某个单元格边框样式后,会有部分样式不生效?


背景:

在使用表格类产品时,数据填报是一个常见的场景。在这个场景中,用户需要设计一个模板表格,其他用户按照该模板表格填写相应的信息。

为了使模板表格更加清晰美观,添加单元格边框是非常必要的。漂亮的边框样式不仅可以为用户提供明确的填报路径,还能让阅读者清楚了解需要填报的内容。

然而,在设计模板时,经常会遇到这样的情况:明明为某个单元格设置了全边框样式,但总有几条边的样式与设计不一致。当通过API查看该单元格的样式时,却没有发现任何异常。这是因为边框样式的优先级可能不同,而页面上往往显示的是优先级较高的边框样式,所以这并不是一个bug。

原理解释

为了确定边框样式的优先级,我们可以进行对比实验。我们使用控制变量法,分别对边框的粗细、样式和线型进行对照。为了排除相邻单元格位置的差异,我们对每种样式都进行了两组实验,从而判断出单元格位置对样式造成的影响。

例如,在E列和G列分别进行了以下对比组:颜色对比组、粗细对比组、线型对比组。然后,我们删除了F列,这样删除后的两列单元格就相邻了,可以进一步观察边框样式的效果。

image

通过删除F列我们可以很容易得出如下结论:黑色>红色、粗>细、实线>虚线

image

在实际生产中,我们通常不仅使用一种样式,而是通过三种样式的交叉组合来实现。对于颜色、粗细和线型,我们需要确定哪个样式优先级更高。

我们选择了某种方法,并假设颜色是最高优先级。这意味着无论其他两个样式如何设置,我们都要根据颜色来决定样式。为此,我们将E15单元格设置为黑色、细的虚线,将G15设置为红色、粗的实线。在这里,细的虚线和红色这两个样式是低优先级,而粗的实线和黑色这两个样式是高优先级。因此,颜色是最高优先级,即使G15的红色边框具有不同的样式,我们也会显示E15单元格的黑色边框样式。

image

同样的我分别假设粗细优先级最高、线型优先级最高。从而分别作了如上两种对比实现红色粗虚线和和黑色细实线,红色细实线和黑色粗虚线。

接着我们同样删除F列可以很容易得出如下结论:粗细的优先级是三个样式优先级最高的

按照上面的方法,我们对颜色和线型也进行讨论,最后的出,在Excel中粗细>虚实>颜色

image

我们将上述实验在SpreadJS中进行重现,我们发现在SpreadJS中的边框样式优先级和Excel是不同的,SpreadJS并不会对颜色进行优先级区分,而是对位置进行优先级区分。详细结果如下:

image


样式优先级总结

我们发现Excel和SpreadJS样式优先级大部分是一致的、除了颜色上。我们很清楚的看到我们的在线表格编辑器的颜色优先级上并不是按照黑色>红色来显示、反而是位置起到了决定作用,于是为了详细了解SpreadJS在边框样式上位置对边框的显示,我们专门对SpreadJS做了一个位置的对比实验。

我们将D4设置为红色单元格,上下左右四个方向单元格设置为黑色,除了位置以外、其他都保持一致(颜色是为了区分哪个位置优先显示),然后我们分别将3、5行和C、E列删除,查看边框样式显示

image

我们可以很明显的看出,在SpreadJS中,上面单元格的下边框优先于下面单元格的上边框、左边单元格的右边框优先于右边单元格的左边框

image

综上所述:在SpreadJS中边框优先级为:粗细>线型>位置

解决方案:

如果你记不住边框样式的优先级,或者只想让所有单元格按照你的设想显示,那么我们可以使用一种简单的解决方案。在设置单元格的边框样式之前,我们可以清空与当前单元格相邻的单元格的边框样式,以解决可能出现的样式冲突(在线表格编辑器的UI也是这种方法)。以下是相关代码:

var rowCol = {
  row: 4,
  col: 7,
};// 当前单元格索引

let span = sheet.getSpan(rowCol.row, rowCol.col);// 判断当前单元格是不是合并单元格
let cell = sheet.getCell(
  rowCol.row,
  rowCol.col,
  GC.Spread.Sheets.SheetArea.viewport
);

span = span ? span : cell;

var borderBottom = new GC.Spread.Sheets.LineBorder(null); // 上
sheet
  .getRange(span.row - 1, span.col, 1, span.colCount)
  .setBorder(borderBottom, { bottom: true });// 将上单元格的下边框的样式设置为空

var borderTop = new GC.Spread.Sheets.LineBorder(null); // 下
sheet
  .getRange(span.row + 1, span.col, 1, span.colCount)
  .setBorder(borderTop, { top: true });// 将下单元格的上边框的样式设置为空

var borderLeft = new GC.Spread.Sheets.LineBorder(null); // 右
sheet
  .getRange(span.row, span.col + span.colCount, span.rowCount, 1)
  .setBorder(borderLeft, { left: true });// 将右单元格的左边框的样式设置为空

var borderRight = new GC.Spread.Sheets.LineBorder(null); // 左
sheet
  .getRange(span.row, span.col - 1, span.rowCount, 1)
  .setBorder(borderRight, { right: true });// 将左单元格的右边框的样式设置为空

// 设置当前单元格的样式
var lineStyle = GC.Spread.Sheets.LineStyle.thin;
var lineBorder = new GC.Spread.Sheets.LineBorder("green", lineStyle);
var sheetArea = GC.Spread.Sheets.SheetArea.viewport;
sheet
  .getRange(span.row, span.col, span.rowCount, span.colCount)
  .setBorder(lineBorder, { all: true }, sheetArea);

大家可以将附件的xlsx文件下载到本地进行测试

样式优先级探讨.xlsx