gpt4 book ai didi

google-apps-script - 如果在 Google 表格中满足条件,则将边框格式添加到行

转载 作者:行者123 更新时间:2023-12-04 00:32:29 26 4
gpt4 key购买 nike

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
---------------------------

此问题的脚本/答案应在第 2、4、5 和 6 行的整行(“A”、“B”和“C”列)下显示底部边框。电子表格逻辑为相当简单:

  1. 在单元格C1中,=IF(A1=A2,"NOBORDER","BORDER")
  2. 然后将上面的内容包装在 =IF(C1="BORDER", addBorder(A1:C1), "NOBORDER")

在更大的数据集中,这种格式将帮助最终用户更轻松地查看逻辑分组。需要 Google Apps 脚本,因为数据会动态更新。

下面是一些示例 Google Apps 脚本代码,但它不起作用

function onEdit(event) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = event.range;

var startingRow = range.getRow();
var numRows = range.getNumRows();
for (var i = 1; i <= numRows; i++) {
var rowNbr = startingRow + i - 1;
Logger.log("changed row %s",rowNbr );
//Now check the WHOLE row
var colLeft=1; //1=A
var colRight=11; //11=K
var row = sheet.getRange(rowNbr,colLeft, 1, colRight - colLeft + 1);
if(row.isBlank()) {
row.setBorder(false, null, true, null, null, null, "red", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
} else {
row.setBorder(false, null, null, null, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
}
}
}

最佳答案

在此问答的帮助下弄清楚了:https://webapps.stackexchange.com/questions/59484/changing-row-colour-if-value-set?rq=1

此代码测试 A 列中的值。如果结果 = TRUE,则在整行应用边框:

function onOpen() {
GroupMyData(); // trigger this function on sheet opening
}

function GroupMyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('shared'); // 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
}
}
};

关于google-apps-script - 如果在 Google 表格中满足条件,则将边框格式添加到行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com