[17.1.4] 使用 getRange ,如何讓迭取的範圍外框套用LineBorder
本帖最后由 Wilson.Zhang 于 2024-12-16 15:54 编辑你好,
想詢問,當我選取一个範圍的單元格,如何讓迭取的範圍外圍套用線條样式
目前寫法如下, 但这样寫連中間的線條都套用了,僅希望外圍框線套用.
var activeSheet = spread.getActiveSheet();
var selectedRanges = activeSheet.getSelections();
selectedRanges.forEach(range => {
activeSheet.getRange(range.row, range.col, range.rowCount, range.colCount, GC.Spread.Sheets.SheetArea.viewport).borderLeft(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
activeSheet.getRange(range.row, range.col, range.rowCount, range.colCount, GC.Spread.Sheets.SheetArea.viewport).borderRight(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
activeSheet.getRange(range.row, range.col, range.rowCount, range.colCount, GC.Spread.Sheets.SheetArea.viewport).borderTop(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
activeSheet.getRange(range.row, range.col, range.rowCount, range.colCount, GC.Spread.Sheets.SheetArea.viewport).borderBottom(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
});
您好!對於一塊被選中的單元格區域,作爲一個整體,其上邊框為其内部首行所有單元格的上邊框組成,下邊框為其内部末行所有單元格的下邊框組成,左邊框為其内部首列所有單元格的左邊框組成,右邊框為其内部末列所有右邊框組成。
因此,要針對單元格區域的上下左右邊框分別設置,如下動圖所示:
可以參考如下代碼:
var ls_color_name = 'black';
var selectedRanges = sheet.getSelections();
selectedRanges.forEach(range => {
//設置單元格區域的左邊框
sheet.getRange(range.row, range.col, range.rowCount, 1, GC.Spread.Sheets.SheetArea.viewport)
.borderLeft(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
//設置單元格區域的有邊框
sheet.getRange(range.row, range.col + range.colCount - 1, range.rowCount, 1, GC.Spread.Sheets.SheetArea.viewport)
.borderRight(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
//設置單元格區域的上邊框
sheet.getRange(range.row, range.col, 1, range.colCount, GC.Spread.Sheets.SheetArea.viewport)
.borderTop(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
//設置單元格區域的下邊框
sheet.getRange(range.row + range.rowCount - 1, range.col, 1, range.colCount, GC.Spread.Sheets.SheetArea.viewport)
.borderBottom(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium));
});
此外,還可以一次性對單元格區域的四周設置邊框樣式,可以參考如下代碼:
var ls_color_name = 'black';
selectedRanges = sheet.getSelections();
selectedRanges.forEach(range => {
sheet.getRange(range.row, range.col, range.rowCount, range.colCount)
.setBorder(new GC.Spread.Sheets.LineBorder(ls_color_name, GC.Spread.Sheets.LineStyle.medium),
{ left: true, right: true, top: true, bottom: true});
});
页:
[1]