如果Google表格中符合条件,请将边框格式添加到行

时间:2018-02-27 04:36:42

标签: google-apps-script google-sheets google-sheets-api

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' A'''' C'”列下方显示底部边框对于行:2,4,5和& 6.电子表格逻辑非常简单:

  1. 在单元格C1中,=IF(A1=A2,"NOBORDER","BORDER")
  2. 然后在=IF(C1="BORDER", addBorder(A1:C1), "NOBORDER")
  3. 中包含上述内容

    在更大的数据集中,此格式化将帮助最终用户更轻松地查看逻辑分组。需要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);
        }
      }
    }
    

1 个答案:

答案 0 :(得分:1)

在此Q&amp; A:https://webapps.stackexchange.com/questions/59484/changing-row-colour-if-value-set?rq=1

的帮助下计算出来

此代码测试A列中的值。如果result = 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
      }
  }
};