以编程方式在电子表格中设置边框颜色和样式



Google电子表格在工具栏中的边框按钮下还有一个按钮,用于更改颜色和更改边框样式。

如何在Google Apps Script中访问这些内容?

为文档描述的setBorderColor函数似乎不适用于电子表格。

报告的问题已修复,截至 2016 年 1 月 12 日。范围现在有以下方法:

  • 设置边框(顶部,左侧,底部,右侧,垂直,水平),和以前一样。
  • 设置边框(顶部,左侧,底部,右侧,垂直,水平,颜色,样式),新!

文档中提供了示例;下面介绍了如何设置红色虚线边框*

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B2");
// Sets borders on the top and bottom, but leaves the left and right unchanged
// Also sets the color to "red", and the border to "DASHED".
cell.setBorder(true, null, true, null, false, false, "red", SpreadsheetApp.BorderStyle.DASHED);

*根据评论更正:文档是错误的,它应该是SpreadsheetApp.BorderStyle.DASHED/DOTTED/SOLID,而不是Range。

目前 setBorder() 属性不允许我们提供颜色和样式。您可以在此处关注一个未解决的问题。

你可以做一个小技巧。将彩色边框单元格中的格式复制到所需的任何位置。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var destination = ss.getSheets()[1];
var blueBorderRange = source.getRange("B2:D4");
// This copies the formatting in B2:D4 from the source sheet to
// D4:F6 in the second sheet
blueBorderRange.copyFormatToRange(destination, 4, 6, 4, 6);

最新更新