Google sheets Apps Script: Border rows in order to keep columns with the same value visually separate

huangapple go评论84阅读模式
英文:

Google sheets Apps Script: Border rows in order to keep columns with the same value visually separate

问题

function borderByTask() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var dataStartRow = 4;
  var lastRow = sh.getLastRow();
  var range_input = ss.getRange("F4:F" + lastRow).getValues(); // Updated range input
  var data = range_input.filter(String); // Eliminate empty cells
  // Clear previous bordering
  sh.getRange('A4:S' + lastRow).setBorder(null, null, null, null, null, null, "black", SpreadsheetApp.BorderStyle.NONE); // Clear all borders
  
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i][0] !== data[i+1][0]) {
      // Border the bottom of the cells if task is different below
      sh.getRange(i + dataStartRow, 2, 1, 18).setBorder(null, null, true, null, null, true, "black", SpreadsheetApp.BorderStyle.SOLID_THIN);
    }
  }
}

请尝试使用上面提供的代码,这个版本已经进行了一些修正,应该可以解决你遇到的问题。如果还有其他问题,请告诉我。

英文:
Column A ... Column F ...
Cell ... T1234 ...
Cell ... T1234 ...
Cell ... T1234 ...
Cell ... T1234 ...
Cell ... T1234 ...
Cell ... T1234 ...
______________ ____ ______________ ____
Cell ... T4321 ...
Cell ... T4321 ...
______________ ____ ______________ ____
Cell ... T4567 ...

...

I want to use a script that will partition cells with equivalent column F values like the above example.

function borderByTask() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var dataStartRow = 4;
  var lastRow = sh.getLastRow();
  var range_input = ss.getRange(&quot;F4:F&quot;).getValues();
  var data = range_input.filter(String); //eliminate empty cells
  //clear previous bordering
  sh.getRange(&#39;A4:S&#39;).setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, &quot;black&quot; /*color*/, SpreadsheetApp.BorderStyle.None);//Clear all borders
  Logger.log(data.length);

    for (var i = (dataStartRow - 1); i &lt; data.length; i++) {
      if (data[i][0] != data[i+1][0]) {   // index 2 = 3rd column = C
      Logger.log(data[i][0]);
      //Border the bottom of the cells if task is different below
      //setBorder(top, left, bottom, right, vertical, horizontal, color, style)
      sh.getRange(i, 2, 1, 18).setBorder(false, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, &quot;black&quot; /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
      }
    }
}

**My problem is:

  • TypeError: Cannot read properties of undefined (reading '0')
    borderByTask @ buttonFunctions.gs:85
  • Borders do not get placed on appropriate rows**

答案1

得分: 0

尽管我不确定我是否能正确理解你期望的结果,但以下示例脚本是否是你期望的结果?

示例脚本:

function borderByTask() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  sheet.getRange('A4:S').setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.None);//清除所有边框
  var lastRow = sheet.getLastRow();
  var values = sheet.getRange("F4:F" + lastRow).getDisplayValues();
  var rows = [...values.reduce((m, [f], i) => m.set(f, i + 4), new Map()).values()];
  rows.forEach(e => {
    if (e == lastRow) return;
    sheet.getRange(`A${e}:S${e}`).setBorder(null, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
  });
}

测试:

运行此脚本时,将获得以下结果。

Google sheets Apps Script: Border rows in order to keep columns with the same value visually separate

参考资料:

英文:

Although I'm not sure whether I could correctly understand your expected result, is the following sample script your expected result?

Sample script:

function borderByTask() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  sheet.getRange(&#39;A4:S&#39;).setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, &quot;black&quot; /*color*/, SpreadsheetApp.BorderStyle.None);//Clear all borders
  var lastRow = sheet.getLastRow();
  var values = sheet.getRange(&quot;F4:F&quot; + lastRow).getDisplayValues();
  var rows = [...values.reduce((m, [f], i) =&gt; m.set(f, i + 4), new Map()).values()];
  rows.forEach(e =&gt; {
    if (e == lastRow) return;
    sheet.getRange(`A${e}:S${e}`).setBorder(null, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, &quot;black&quot; /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
  });
}

Testing:

When this script is run, the following result is obtained.

Google sheets Apps Script: Border rows in order to keep columns with the same value visually separate

References:

huangapple
  • 本文由 发表于 2023年6月9日 02:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定