【发布时间】:2018-10-29 20:42:06
【问题描述】:
我正在尝试完全按照Add border format to row if condition met in Google Sheets 的要求去做,但接受的答案根本不起作用。原帖说的很清楚了,这里再放一遍:
Excel 显然具有这种类型的条件格式 built-in.
我想通过Google Apps Script 在 Google 表格中完成相同的操作。下面应该演示之前和之后 条件:
示例数据集
A B C 1 apple Macintosh 2 apple Granny Smith 3 orange Florida 4 orange Valencia 5 pear Garden 6 banana Chiquita生成的示例数据集
A B C 1 apple Macintosh 2 apple Granny Smith ----------------------------------- 3 orange Florida 4 orange Valencia ----------------------------------- 5 pear Garden ----------------------------------- 6 banana Chiquita -----------------------------------此问题的脚本/答案应显示底部边框 在整行(列“A”、“B”和“C”)下,第 2、4、5 和 6 行。
这个原始帖子的接受答案提供了以下脚本:
function onOpen() {
GroupMyData(); // trigger this function on sheet opening
}
function GroupMyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet 1'); // apply to sheet name only
var rows = sheet.getRange('a1:g'); // range to apply formatting to
var numRows = rows.getNumRows(); // no. of rows in the range named above
var values = rows.getValues(); // array of values in the range named above
var testvalues = sheet.getRange('a1:a').getValues(); // array of values to be tested (1st column of the range named above)
rows.setBorder(false, false, false, false, false, false, "red", SpreadsheetApp.BorderStyle.SOLID_MEDIUM); // remove existing borders before applying rule below
//Logger.log(numRows);
for (var i = 0; i <= numRows - 1; i++) {
var n = i + 1;
//Logger.log(n);
//Logger.log(testvalues[i] > 0);
//Logger.log(testvalues[i]);
if (testvalues[i] > 0) { // test applied to array of values
sheet.getRange('a' + n + ':g' + n).setBorder(null, null, true, null, null, null, "red", SpreadsheetApp.BorderStyle.SOLID_MEDIUM); // format if true
}
}
};
该脚本的功能如下: 每一行的底部都会出现一个边框,其中 A 列的单元格中有一个数字(并且没有字母),无论该数字是否与其上方单元格中的数字相同或不同。
但是,A 的目标是要有文本,并且当 A 列下的连续文本从重复字符串变为不同字符串时,不同字符串之间应该有一个边框。
【问题讨论】:
标签: google-apps-script google-sheets google-sheets-api